前言
應用程序啟動時有些會有引導頁,目的是用戶第一次登錄時對應用程序的一些簡單了解介紹,一般就是幾張輪播圖片,當引用程序第一次進入時會跳到引導頁,以后不再顯示,這時就需要將不是第一次登錄的標致flag保存到內存中,推薦用戶偏好設置NSUserDefaults,第一直接去取值取這個flag取不到(因為是第一次登錄)就跳引導頁,然后在引導頁進入登錄頁或者首頁時將flag值保存到偏好設置中,以后再進來就可以取到不是第一登錄的flag就直接跳過引導頁.方式有兩種:一種是直接切換UIWindow的根控制器本文是第一種,另一種是模態彈出,根據具體需求決定!
效果圖:

引導頁及指紋識別效果圖1

引導頁及指紋識別效果圖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{//否則直接進入登錄頁面
self.window.rootViewController = [[LoginViewController alloc]init];
}
[self.window makeKeyAndVisible];
return YES;
}
引導頁控制器:GuidePagesViewController
//
// GuidePagesViewController.m
// 登錄引導頁開發
//
// 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){//最后一張圖片時添加點擊進入按鈕
imageV.userInteractionEnabled = YES;
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(ScreenWidth * 0.5 - 80, ScreenHeight * 0.7, 160, 40);
[btn setTitle:@"點擊一下,你就知道" 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];
}
}
}
//點擊按鈕保存第一次登錄的標記到本地并且跳入登錄界面
- (void)btnClick{
//保存標記到本地
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)];
//設置總頁數
_pageControl.numberOfPages = self.images.count;
//設置分頁指示器顏色
_pageControl.pageIndicatorTintColor = [UIColor blueColor];
//設置當前指示器顏色
_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
指紋解鎖很簡單,導入頭文件#import "LocalAuthentication/LocalAuthentication.h",驗證手機系統是否支持指紋解鎖 iOS 8以后才行,驗證本手機是否開啟了指紋識別,是否錄入了指紋等
指紋登錄驗證:LoginViewController
//
// LoginViewController.m
// 指紋驗證
//
// 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:@"點擊一下,指紋登錄" 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
{
//創建LAContext
LAContext* context = [[LAContext alloc] init];
NSError* error = nil;
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//支持指紋驗證
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"請驗證已有指紋" reply:^(BOOL success, NSError *error) {
if (success) {
//驗證成功,主線程處理UI
NSLog(@"成功啦");
//用戶選擇輸入密碼,切換主線程處理
dispatch_async(dispatch_get_main_queue(), ^{
[self showMessage:@"指紋登錄成功!"];
});
}
else
{
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
[self showMessage:@"系統取消授權,如其他APP切入"];
//系統取消授權,如其他APP切入
break;
}
case LAErrorUserCancel:
{
//用戶取消驗證Touch ID
[self showMessage:@"用戶取消驗證Touch ID"];
break;
}
case LAErrorAuthenticationFailed:
{
//授權失敗
[self showMessage:@"授權失敗"];
break;
}
case LAErrorPasscodeNotSet:
{
//系統未設置密碼
[self showMessage:@"系統未設置密碼"];
break;
}
case LAErrorBiometryNotAvailable:
{
//設備Touch ID不可用,例如未打開
[self showMessage:@"設備Touch ID不可用,例如未打開"];
break;
}
case LAErrorBiometryNotEnrolled:
{
//設備Touch ID不可用,用戶未錄入
[self showMessage:@"設備Touch ID不可用,用戶未錄入"];
break;
}
case LAErrorUserFallback:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//用戶選擇輸入密碼,切換主線程處理
[self showMessage:@"用戶選擇輸入密碼,切換主線程處理"];
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情況,切換主線程處理
[self showMessage:@"其他情況,切換主線程處理"];
}];
break;
}
}
}
}];
}
else
{
//不支持指紋識別,LOG出錯誤詳情
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
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。