iOS實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁與指紋解鎖的方法詳解

前言

創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的平武網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

應(yīng)用程序啟動(dòng)時(shí)有些會(huì)有引導(dǎo)頁,目的是用戶第一次登錄時(shí)對應(yīng)用程序的一些簡單了解介紹,一般就是幾張輪播圖片,當(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ù)具體需求決定!

效果圖:

iOS實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁與指紋解鎖的方法詳解

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

iOS實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁與指紋解鎖的方法詳解

引導(dǎo)頁及指紋識別效果圖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

指紋解鎖很簡單,導(dǎo)入頭文件#import "LocalAuthentication/LocalAuthentication.h",驗(yàn)證手機(jī)系統(tǒng)是否支持指紋解鎖 iOS 8以后才行,驗(yàn)證本手機(jī)是否開啟了指紋識別,是否錄入了指紋等

指紋登錄驗(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:@"請驗(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
 {
  //不支持指紋識別,LOG出錯(cuò)誤詳情
  NSLog(@"不支持指紋識別");
  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é)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。

網(wǎng)頁題目:iOS實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁與指紋解鎖的方法詳解
當(dāng)前鏈接:http://muchs.cn/article44/jejgee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、小程序開發(fā)網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站營銷虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司