查看: 300|回复: 0

[IOS开发教程] iOS实现启动引导页与指纹解锁的方法详解

发表于 2018-4-13 08:00:05

前言

应用程序启动时有些会有引导页,目的是用户第一次登录时对应用程序的一些简单了解介绍,一般就是几张轮播图片,当引用程序第一次进入时会跳到引导页,以后不再显示,这时就需要将不是第一次登录的标致flag保存到内存中,推荐用户偏好设置NSUserDefaults,第一直接去取值取这个flag取不到(因为是第一次登录)就跳引导页,然后在引导页进入登录页或者首页时将flag值保存到偏好设置中,以后再进来就可以取到不是第一登录的flag就直接跳过引导页.方式有两种:一种是直接切换UIWindow的根控制器本文是第一种,另一种是模态弹出,根据具体需求决定!

效果图:

引导页及指纹识别效果图1

引导页及指纹识别效果图2

以下直接上代码:

AppDelegate文件中

  1. #import "AppDelegate.h"
  2. #import "GuidePagesViewController.h"
  3. #import "LoginViewController.h"
  4. @interface AppDelegate ()
  5. @end
  6. @implementation AppDelegate
  7. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  8. self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  9. self.window.backgroundColor = [UIColor whiteColor];
  10. NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
  11. if (![userDefault boolForKey:@"isNotFirst"]) {//如果用户是第一次登录
  12. self.window.rootViewController = [[GuidePagesViewController alloc]init];
  13. }else{//否则直接进入登录页面
  14. self.window.rootViewController = [[LoginViewController alloc]init];
  15. }
  16. [self.window makeKeyAndVisible];
  17. return YES;
  18. }
复制代码

引导页控制器:GuidePagesViewController

  1. //
  2. // GuidePagesViewController.m
  3. // 登录引导页开发
  4. //
  5. // Created by hj on 2018/1/31.
  6. // Copyright ? 2018年 hj. All rights reserved.
  7. //
  8. #import "GuidePagesViewController.h"
  9. #import "LoginViewController.h"
  10. #define ScreenWidth [UIScreen mainScreen].bounds.size.width
  11. #define ScreenHeight [UIScreen mainScreen].bounds.size.height
  12. @interface GuidePagesViewController ()<UIScrollViewDelegate>
  13. @property(nonatomic ,strong) UIScrollView * mainScrollV;
  14. @property(nonatomic ,strong) UIPageControl * pageControl;
  15. @property(nonatomic ,strong) NSMutableArray * images;
  16. @end
  17. @implementation GuidePagesViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. [self.view addSubview:self.mainScrollV];
  21. [self.view addSubview:self.pageControl];
  22. }
  23. -(UIScrollView *)mainScrollV{
  24. if (!_mainScrollV) {
  25. _mainScrollV = [[UIScrollView alloc]initWithFrame:self.view.bounds];
  26. _mainScrollV.bounces = NO;
  27. _mainScrollV.pagingEnabled = YES;
  28. _mainScrollV.showsHorizontalScrollIndicator = NO;
  29. _mainScrollV.delegate = self;
  30. _mainScrollV.contentSize = CGSizeMake(self.images.count * ScreenWidth, ScreenHeight);
  31. [self addSubImageViews];
  32. }
  33. return _mainScrollV;
  34. }
  35. -(NSMutableArray *)images{
  36. if (!_images) {
  37. _images = [NSMutableArray array];
  38. NSArray * imageNames = @[@"u1",@"u2",@"u3",@"u4"];
  39. for (NSString * name in imageNames) {
  40. [self.images addObject:[UIImage imageNamed:name]];
  41. }
  42. }
  43. return _images;
  44. }
  45. - (void)addSubImageViews{
  46. for (int i = 0; i < self.images.count; i++) {
  47. UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(i * ScreenWidth, 0, ScreenWidth, ScreenHeight)];
  48. imageV.image = self.images[i];
  49. [_mainScrollV addSubview:imageV];
  50. if (i == self.images.count - 1){//最后一张图片时添加点击进入按钮
  51. imageV.userInteractionEnabled = YES;
  52. UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
  53. btn.frame = CGRectMake(ScreenWidth * 0.5 - 80, ScreenHeight * 0.7, 160, 40);
  54. [btn setTitle:@"点击一下,你就知道" forState:UIControlStateNormal];
  55. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  56. btn.backgroundColor = [UIColor redColor];
  57. btn.layer.cornerRadius = 20;
  58. btn.layer.borderWidth = 1;
  59. btn.layer.borderColor = [UIColor redColor].CGColor;
  60. [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
  61. [imageV addSubview:btn];
  62. }
  63. }
  64. }
  65. //点击按钮保存第一次登录的标记到本地并且跳入登录界面
  66. - (void)btnClick{
  67. //保存标记到本地
  68. NSUserDefaults * userDef = [NSUserDefaults standardUserDefaults];
  69. [userDef setBool:YES forKey:@"isNotFirst"];
  70. [userDef synchronize];
  71. //切换视图控制器
  72. [UIApplication sharedApplication].keyWindow.rootViewController = [[LoginViewController alloc]init];
  73. }
  74. -(UIPageControl *)pageControl{
  75. if (!_pageControl) {
  76. _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(ScreenWidth/self.images.count, ScreenHeight * 15/16.0, ScreenWidth/2, ScreenHeight/16.0)];
  77. //设置总页数
  78. _pageControl.numberOfPages = self.images.count;
  79. //设置分页指示器颜色
  80. _pageControl.pageIndicatorTintColor = [UIColor blueColor];
  81. //设置当前指示器颜色
  82. _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
  83. _pageControl.enabled = NO;
  84. }
  85. return _pageControl;
  86. }
  87. #pragma mark UIScrollViewDelegate
  88. -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  89. self.pageControl.currentPage = (NSInteger)self.mainScrollV.contentOffset.x/ScreenWidth;
  90. }
  91. @end
复制代码

指纹解锁很简单,导入头文件#import "LocalAuthentication/LocalAuthentication.h",验证手机系统是否支持指纹解锁 iOS 8以后才行,验证本手机是否开启了指纹识别,是否录入了指纹等

指纹登录验证:LoginViewController

  1. //
  2. // LoginViewController.m
  3. // 指纹验证
  4. //
  5. // Created by hj on 2018/1/31.
  6. // Copyright ? 2018年 hj. All rights reserved.
  7. //
  8. #import "LoginViewController.h"
  9. #import "LocalAuthentication/LocalAuthentication.h"
  10. @interface LoginViewController ()
  11. @end
  12. @implementation LoginViewController
  13. - (void)viewDidLoad {
  14. [super viewDidLoad];
  15. if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {//8.0以后才支持指纹
  16. return;
  17. }
  18. UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
  19. btn.frame = CGRectMake(0, 0, 160, 50);
  20. btn.center = self.view.center;
  21. [btn setTitle:@"点击一下,指纹登录" forState:0];
  22. [btn setTitleColor:[UIColor redColor] forState:0];
  23. btn.backgroundColor = [UIColor yellowColor];
  24. btn.layer.borderColor = [UIColor orangeColor].CGColor;
  25. btn.layer.borderWidth = 2;
  26. btn.layer.cornerRadius = 20;
  27. [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
  28. [self.view addSubview:btn];
  29. }
  30. - (void)btnClick{
  31. [self fingerprintVerification];
  32. }
  33. - (void)fingerprintVerification
  34. {
  35. //创建LAContext
  36. LAContext* context = [[LAContext alloc] init];
  37. NSError* error = nil;
  38. if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
  39. //支持指纹验证
  40. [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请验证已有指纹" reply:^(BOOL success, NSError *error) {
  41. if (success) {
  42. //验证成功,主线程处理UI
  43. NSLog(@"成功啦");
  44. //用户选择输入密码,切换主线程处理
  45. dispatch_async(dispatch_get_main_queue(), ^{
  46. [self showMessage:@"指纹登录成功!"];
  47. });
  48. }
  49. else
  50. {
  51. NSLog(@"%@",error.localizedDescription);
  52. switch (error.code) {
  53. case LAErrorSystemCancel:
  54. {
  55. [self showMessage:@"系统取消授权,如其他APP切入"];
  56. //系统取消授权,如其他APP切入
  57. break;
  58. }
  59. case LAErrorUserCancel:
  60. {
  61. //用户取消验证Touch ID
  62. [self showMessage:@"用户取消验证Touch ID"];
  63. break;
  64. }
  65. case LAErrorAuthenticationFailed:
  66. {
  67. //授权失败
  68. [self showMessage:@"授权失败"];
  69. break;
  70. }
  71. case LAErrorPasscodeNotSet:
  72. {
  73. //系统未设置密码
  74. [self showMessage:@"系统未设置密码"];
  75. break;
  76. }
  77. case LAErrorBiometryNotAvailable:
  78. {
  79. //设备Touch ID不可用,例如未打开
  80. [self showMessage:@"设备Touch ID不可用,例如未打开"];
  81. break;
  82. }
  83. case LAErrorBiometryNotEnrolled:
  84. {
  85. //设备Touch ID不可用,用户未录入
  86. [self showMessage:@"设备Touch ID不可用,用户未录入"];
  87. break;
  88. }
  89. case LAErrorUserFallback:
  90. {
  91. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  92. //用户选择输入密码,切换主线程处理
  93. [self showMessage:@"用户选择输入密码,切换主线程处理"];
  94. }];
  95. break;
  96. }
  97. default:
  98. {
  99. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  100. //其他情况,切换主线程处理
  101. [self showMessage:@"其他情况,切换主线程处理"];
  102. }];
  103. break;
  104. }
  105. }
  106. }
  107. }];
  108. }
  109. else
  110. {
  111. //不支持指纹识别,LOG出错误详情
  112. NSLog(@"不支持指纹识别");
  113. switch (error.code) {
  114. case LAErrorBiometryNotEnrolled:
  115. {
  116. NSLog(@"TouchID is not enrolled");
  117. [self showMessage:@"TouchID is not enrolled"];
  118. break;
  119. }
  120. case LAErrorPasscodeNotSet:
  121. {
  122. NSLog(@"A passcode has not been set");
  123. [self showMessage:@"A passcode has not been set"];
  124. break;
  125. }
  126. default:
  127. {
  128. NSLog(@"TouchID not available");
  129. [self showMessage:@"TouchID not available"];
  130. break;
  131. }
  132. }
  133. NSLog(@"error : %@",error.localizedDescription);
  134. }
  135. }
  136. -(void)showMessage:(NSString *)msg{
  137. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  138. [alert show];
  139. }
  140. @end
复制代码

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对程序员之家的支持。



回复

使用道具 举报