iOS實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁與指紋解鎖的方法詳解
前言
應(yīng)用程序啟動(dòng)時(shí)有些會(huì)有引導(dǎo)頁,目的是用戶第一次登錄時(shí)對(duì)應(yīng)用程序的一些簡(jiǎn)單了解介紹,一般就是幾張輪播圖片,當(dāng)引用程序第一次進(jìn)入時(shí)會(huì)跳到引導(dǎo)頁,以后不再顯示,這時(shí)就需要將不是第一次登錄的標(biāo)致flag保存到內(nèi)存中,推薦用戶偏好設(shè)置NSUserDefaults,第一直接去取值取這個(gè)flag取不到(因?yàn)槭堑谝淮蔚卿?就跳引導(dǎo)頁,然后在引導(dǎo)頁進(jìn)入登錄頁或者首頁時(shí)將flag值保存到偏好設(shè)置中,以后再進(jìn)來就可以取到不是第一登錄的flag就直接跳過引導(dǎo)頁.方式有兩種:一種是直接切換UIWindow的根控制器本文是第一種,另一種是模態(tài)彈出,根據(jù)具體需求決定!
效果圖:

引導(dǎo)頁及指紋識(shí)別效果圖1

引導(dǎo)頁及指紋識(shí)別效果圖2
以下直接上代碼:
AppDelegate文件中
#import "AppDelegate.h"
#import "GuidePagesViewController.h"
#import "LoginViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
if (![userDefault boolForKey:@"isNotFirst"]) {//如果用戶是第一次登錄
self.window.rootViewController = [[GuidePagesViewController alloc]init];
}else{//否則直接進(jìn)入登錄頁面
self.window.rootViewController = [[LoginViewController alloc]init];
}
[self.window makeKeyAndVisible];
return YES;
}
引導(dǎo)頁控制器:GuidePagesViewController
//
// GuidePagesViewController.m
// 登錄引導(dǎo)頁開發(fā)
//
// Created by hj on 2018/1/31.
// Copyright © 2018年 hj. All rights reserved.
//
#import "GuidePagesViewController.h"
#import "LoginViewController.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface GuidePagesViewController ()<UIScrollViewDelegate>
@property(nonatomic ,strong) UIScrollView * mainScrollV;
@property(nonatomic ,strong) UIPageControl * pageControl;
@property(nonatomic ,strong) NSMutableArray * images;
@end
@implementation GuidePagesViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.mainScrollV];
[self.view addSubview:self.pageControl];
}
-(UIScrollView *)mainScrollV{
if (!_mainScrollV) {
_mainScrollV = [[UIScrollView alloc]initWithFrame:self.view.bounds];
_mainScrollV.bounces = NO;
_mainScrollV.pagingEnabled = YES;
_mainScrollV.showsHorizontalScrollIndicator = NO;
_mainScrollV.delegate = self;
_mainScrollV.contentSize = CGSizeMake(self.images.count * ScreenWidth, ScreenHeight);
[self addSubImageViews];
}
return _mainScrollV;
}
-(NSMutableArray *)images{
if (!_images) {
_images = [NSMutableArray array];
NSArray * imageNames = @[@"u1",@"u2",@"u3",@"u4"];
for (NSString * name in imageNames) {
[self.images addObject:[UIImage imageNamed:name]];
}
}
return _images;
}
- (void)addSubImageViews{
for (int i = 0; i < self.images.count; i++) {
UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(i * ScreenWidth, 0, ScreenWidth, ScreenHeight)];
imageV.image = self.images[i];
[_mainScrollV addSubview:imageV];
if (i == self.images.count - 1){//最后一張圖片時(shí)添加點(diǎn)擊進(jìn)入按鈕
imageV.userInteractionEnabled = YES;
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(ScreenWidth * 0.5 - 80, ScreenHeight * 0.7, 160, 40);
[btn setTitle:@"點(diǎn)擊一下,你就知道" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor redColor];
btn.layer.cornerRadius = 20;
btn.layer.borderWidth = 1;
btn.layer.borderColor = [UIColor redColor].CGColor;
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[imageV addSubview:btn];
}
}
}
//點(diǎn)擊按鈕保存第一次登錄的標(biāo)記到本地并且跳入登錄界面
- (void)btnClick{
//保存標(biāo)記到本地
NSUserDefaults * userDef = [NSUserDefaults standardUserDefaults];
[userDef setBool:YES forKey:@"isNotFirst"];
[userDef synchronize];
//切換視圖控制器
[UIApplication sharedApplication].keyWindow.rootViewController = [[LoginViewController alloc]init];
}
-(UIPageControl *)pageControl{
if (!_pageControl) {
_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(ScreenWidth/self.images.count, ScreenHeight * 15/16.0, ScreenWidth/2, ScreenHeight/16.0)];
//設(shè)置總頁數(shù)
_pageControl.numberOfPages = self.images.count;
//設(shè)置分頁指示器顏色
_pageControl.pageIndicatorTintColor = [UIColor blueColor];
//設(shè)置當(dāng)前指示器顏色
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
_pageControl.enabled = NO;
}
return _pageControl;
}
#pragma mark UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
self.pageControl.currentPage = (NSInteger)self.mainScrollV.contentOffset.x/ScreenWidth;
}
@end
指紋解鎖很簡(jiǎn)單,導(dǎo)入頭文件#import "LocalAuthentication/LocalAuthentication.h",驗(yàn)證手機(jī)系統(tǒng)是否支持指紋解鎖 iOS 8以后才行,驗(yàn)證本手機(jī)是否開啟了指紋識(shí)別,是否錄入了指紋等
指紋登錄驗(yàn)證:LoginViewController
//
// LoginViewController.m
// 指紋驗(yàn)證
//
// Created by hj on 2018/1/31.
// Copyright © 2018年 hj. All rights reserved.
//
#import "LoginViewController.h"
#import "LocalAuthentication/LocalAuthentication.h"
@interface LoginViewController ()
@end
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {//8.0以后才支持指紋
return;
}
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 160, 50);
btn.center = self.view.center;
[btn setTitle:@"點(diǎn)擊一下,指紋登錄" forState:0];
[btn setTitleColor:[UIColor redColor] forState:0];
btn.backgroundColor = [UIColor yellowColor];
btn.layer.borderColor = [UIColor orangeColor].CGColor;
btn.layer.borderWidth = 2;
btn.layer.cornerRadius = 20;
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnClick{
[self fingerprintVerification];
}
- (void)fingerprintVerification
{
//創(chuàng)建LAContext
LAContext* context = [[LAContext alloc] init];
NSError* error = nil;
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//支持指紋驗(yàn)證
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"請(qǐng)驗(yàn)證已有指紋" reply:^(BOOL success, NSError *error) {
if (success) {
//驗(yàn)證成功,主線程處理UI
NSLog(@"成功啦");
//用戶選擇輸入密碼,切換主線程處理
dispatch_async(dispatch_get_main_queue(), ^{
[self showMessage:@"指紋登錄成功!"];
});
}
else
{
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
[self showMessage:@"系統(tǒng)取消授權(quán),如其他APP切入"];
//系統(tǒng)取消授權(quán),如其他APP切入
break;
}
case LAErrorUserCancel:
{
//用戶取消驗(yàn)證Touch ID
[self showMessage:@"用戶取消驗(yàn)證Touch ID"];
break;
}
case LAErrorAuthenticationFailed:
{
//授權(quán)失敗
[self showMessage:@"授權(quán)失敗"];
break;
}
case LAErrorPasscodeNotSet:
{
//系統(tǒng)未設(shè)置密碼
[self showMessage:@"系統(tǒng)未設(shè)置密碼"];
break;
}
case LAErrorBiometryNotAvailable:
{
//設(shè)備Touch ID不可用,例如未打開
[self showMessage:@"設(shè)備Touch ID不可用,例如未打開"];
break;
}
case LAErrorBiometryNotEnrolled:
{
//設(shè)備Touch ID不可用,用戶未錄入
[self showMessage:@"設(shè)備Touch ID不可用,用戶未錄入"];
break;
}
case LAErrorUserFallback:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//用戶選擇輸入密碼,切換主線程處理
[self showMessage:@"用戶選擇輸入密碼,切換主線程處理"];
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情況,切換主線程處理
[self showMessage:@"其他情況,切換主線程處理"];
}];
break;
}
}
}
}];
}
else
{
//不支持指紋識(shí)別,LOG出錯(cuò)誤詳情
NSLog(@"不支持指紋識(shí)別");
switch (error.code) {
case LAErrorBiometryNotEnrolled:
{
NSLog(@"TouchID is not enrolled");
[self showMessage:@"TouchID is not enrolled"];
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"A passcode has not been set");
[self showMessage:@"A passcode has not been set"];
break;
}
default:
{
NSLog(@"TouchID not available");
[self showMessage:@"TouchID not available"];
break;
}
}
NSLog(@"error : %@",error.localizedDescription);
}
}
-(void)showMessage:(NSString *)msg{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alert show];
}
@end
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS開發(fā)-調(diào)用系統(tǒng)相機(jī)和相冊(cè)獲取照片示例
這篇文章主要介紹了iOS開發(fā)-調(diào)用系統(tǒng)相機(jī)和相冊(cè)獲取照片示例的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
iOS開發(fā)中使用CoreLocation框架處理地理編碼的方法
這篇文章主要介紹了iOS開發(fā)中使用CoreLocation框架處理地理編碼的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
IOS開發(fā)仿微信右側(cè)彈出視圖實(shí)現(xiàn)
這篇文章主要介紹了IOS開發(fā)仿微信右側(cè)彈出視圖實(shí)現(xiàn)的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣類似的功能,需要的朋友可以參考下2017-10-10
ios獲取數(shù)據(jù)之encodeURI和decodeURI的實(shí)例
下面小編就為大家?guī)硪黄猧os獲取數(shù)據(jù)之encodeURI和decodeURI的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11

