前言
应用程序启动时有些会有引导页,目的是用户第一次登录时对应用程序的一些简单了解介绍,一般就是几张轮播图片,当引用程序第一次进入时会跳到引导页,以后不再显示,这时就需要将不是第一次登录的标致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