前言
應用程序啟動時有些會有引導頁,目的是用戶第一次登錄時對應用程序的一些簡單了解介紹,一般就是幾張輪播圖片,當引用程序第一次進入時會跳到引導頁,以后不再顯示,這時就需要將不是第一次登錄的標致flag保存到內存中,推薦用戶偏好設置nsuserdefaults,第一直接去取值取這個flag取不到(因為是第一次登錄)就跳引導頁,然后在引導頁進入登錄頁或者首頁時將flag值保存到偏好設置中,以后再進來就可以取到不是第一登錄的flag就直接跳過引導頁.方式有兩種:一種是直接切換uiwindow的根控制器本文是第一種,另一種是模態彈出,根據具體需求決定!
效果圖:
引導頁及指紋識別效果圖1
引導頁及指紋識別效果圖2
以下直接上代碼:
appdelegate文件中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
// // 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// // 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 |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.jianshu.com/p/b28fc638c92f