1.指纹解锁
1.1使用
TouchID的使用很简单,适用于iOS8以上,iPhone5S以上,引入框架
#import <LocalAuthentication/LocalAuthentication.h>
步骤:
1.创建实例 LAContext;
2.判断是否支持TouchID验证;
3.解锁结果判断
#import <LocalAuthentication/LocalAuthentication.h>
- (void)authenticationWithTouchID {
//*1.创建实例
LAContext *context = [[LAContext alloc] init];
//设置取消按钮显示名称,iOS10.0以下为默认显示"Cancel"/"取消"
context.localizedCancelTitle = @"取消";
//自定义回退按钮显示名称,默认"Enter Password"/"输入密码",如果设置为空字符串@"",则会隐藏.
context.localizedFallbackTitle = @"使用密码登陆";
//最大验证失败次数,iOS9.0以上已弃用
//context.maxBiometryFailures = @5;
//NSTimeInterval类型,默认0,最大300,如果复用此context解锁成功后,在设置的这个值内再次验证则会自动验证成功.
//context重新创建后,不起作用.如果需要用到这个功能,可以讲context设为属性.
context.touchIDAuthenticationAllowableReuseDuration = 60;
//*2.判断是否支持TouchID
//- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none)));
//该方法判断是否支持TouchID,其中参数LAPolicy使用策略有两个枚举值:
//LAPolicyDeviceOwnerAuthenticationWithBiometrics, 使用TouchID
//LAPolicyDeviceOwnerAuthentication, 使用TouchID或设备密码
NSError *error = nil;//所有的错误枚举已经在下面列出
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
NSLog(@"支持指纹识别");
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"解锁啦啦啦" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"解锁成功");
dispatch_async(dispatch_get_main_queue(), ^{
//主线程中更新UI
});
} else {
NSLog(@"解锁失败:%@, ->%ld", error.localizedDescription, error.code);
switch (error.code) {
case LAErrorAuthenticationFailed:
NSLog(@"验证失败,多次验证失败");
break;
case LAErrorUserCancel:
NSLog(@"取消Touch ID验证");
break;
case LAErrorUserFallback:
NSLog(@"按了FallBack键,如密码登录");
break;
case LAErrorSystemCancel:
NSLog(@"系统取消验证,主动进入后台,如另一个应用到前台运行(点击通知进入其他应用/点击home键)");
break;
case LAErrorAppCancel:
NSLog(@"应用取消验证,被动进入后台,如接到电话");
break;
default:
break;
}
}
}];
} else {
NSLog(@"不支持指纹,error:%@, ->%ld", error.localizedDescription, error.code);
switch (error.code) {
case LAErrorPasscodeNotSet:
NSLog(@"未设置密码,在设置中关闭了密码");
break;
case LAErrorTouchIDNotAvailable:
NSLog(@"Touch ID不可用, 机型或系统不支持");
break;
case LAErrorTouchIDNotEnrolled:
NSLog(@"Touch ID未录入");
break;
case LAErrorTouchIDLockout://iOS9.0以上系统可用
//LAPolicyDeviceOwnerAuthentication,允许密码和TouchID验证,弹出设备密码验证页面,不会执行到这里
//LAPolicyDeviceOwnerAuthenticationWithBiometrics,仅允许TouchID验证执行到这里,可以弹出提示框
NSLog(@"多次验证错误,Touch ID验证被锁定,需密码解锁");
break;
case LAErrorInvalidContext:
//如果在调用[context invalidate]后再验证,则会执行到这里
NSLog(@"context无效");
break;
default:
NSLog(@"TouchID不可用");
break;
}
}
}
1.2应用
1.2.1只需在在进入应用时进行TouchID验证
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self authenticationWithTouchID];
return YES;
}
1.2.2在应用进入前台时验证
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
NSLog(@"将要进入前台");
[self authenticationWithTouchID];
}
1.2.3任意界面验证
在支付/登陆等场景下直接调用即可
2.手势密码
3.双击home键后,应用页面模糊
这个功能常应用在金融类APP中,个人感觉这个功能对于安全性来说没啥大作用,支付宝之前有,但是新版本中已经没有这个效果了.不管作用多大,先来实现一下.
这个功能涉及的知识点有:1.屏幕截图;2.图片模糊;3.在AppDelegate的UIApplicationDelegate代理方法中添加和移除视图
这里先来说一个效果不太理想的方法:
使用iOS8.0开放的API来实现
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
effectView.frame = [UIScreen mainScreen].bounds;
[[UIApplication sharedApplication].keyWindow addSubview:effectView];
可以看看效果对比图:
原图和效果图



最右边的是我的应用,和其他应用对比,模糊程度太大.
3.1屏幕截图
现在使用屏幕截图,图片高斯模糊再添加到window上的方法.
关于屏幕截图的方法,网上一搜有很多.但是这里有一个注意点,一定要在视图完全显示之后再去截图,否则截图不完整.
在写Demo时,我在- (void)viewDidLoad
方法中进行截图,发现不能截图完整,这里花费了我很多时间,甚至想各个视图分别截图再合成一张图片的方法,这样做很麻烦.
在- (void)viewDidAppear:(BOOL)animated
中截图就可以.
在实际项目场景中这个问题应该不会存在,界面都是现实完全了.
-(UIImage *)fullScreenshot {
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
UIWindow *screenWindow = appDelegate.window;
UIGraphicsBeginImageContext(screenWindow.frame.size);
[[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
3.2模糊
模糊的方法网上文章也很多.
先使用CoreImage的高斯模糊方法
- (UIImage *)blurImage:(UIImage *)originImage {
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *image = [CIImage imageWithCGImage:originImage.CGImage];
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey, image, @"inputRadius", @(5), nil];
CIImage *outputImage = filter.outputImage;
CGImageRef imageRef = [context createCGImage:outputImage fromRect:outputImage.extent];
UIImage * blurImage = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);
return blurImage;
}
正在写关于CoreImage的使用,在以后的文章中会详细介绍.
https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIGaussianBlur
直接看效果:可以看到图片缩小了.据说是iOS8.0以上会出现,没有下载iOS7的模拟器,所以测不了.

下面来优化一下:
- (UIImage *)blurImageView:(UIImage *)originImage {
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *image = [CIImage imageWithCGImage:originImage.CGImage];
CIImage *filtered = [image imageByClampingToExtent];
filtered = [filtered imageByApplyingFilter:@"CIGaussianBlur" withInputParameters:@{kCIInputRadiusKey:@5}];
filtered = [filtered imageByCroppingToRect:image.extent];
CGImageRef outImage = [context createCGImage:filtered fromRect:image.extent];
UIImage * blurImage = [UIImage imageWithCGImage:outImage];
CGImageRelease(outImage);
return blurImage;
}
3.3在合适的时候调用
需求的场景是:当我的应用在后台时,双击home键显示所有后台程序,此时我的程序页面是模糊显示.当我的应用在前台运行时,此时双击home键显示所有后台程序时,页面不模糊显示.
很容易想到,就是在UIApplicationDelegate的代理方法中执行相应方法.
//在进入后台时,添加模糊视图
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
NSLog(@"进入后台");
UIImage *originImage = [self fullScreenshot];
UIImage *blurImage = [self blurImage:originImage];
UIImageView *iView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
iView.contentMode = UIViewContentModeScaleAspectFill;
iView.image = blurImage;
iView.tag = 100;
[self.window addSubview:iView];
}
//在进入前台时移除
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
NSLog(@"进入前台");
UIImageView *iView = [self.window viewWithTag:100];
[iView removeFromSuperview];
iView = nil;
}
看效果好了很多

网友评论