iOS开发基础101-指纹和面部识别
1.iOS开发基础77-一像素线的几种实现方式2.iOS开发基础78-iOS 国际化3.iOS开发基础82-关于iOS目录4.iOS开发基础81-Runtime实战5.iOS开发基础80-关于Xcode86.iOS开发基础79-强制退出程序7.iOS开发基础90-密码学8.iOS开发基础89-Runloop9.iOS开发基础88-涂鸦效果10.iOS开发基础87-抽屉效果11.iOS开发基础86-FMDB12.iOS开发基础85-线程dispatch apply13.iOS开发基础84-HTTP请求方法详解与增删改查的应用14.iOS开发基础83-线程组15.iOS开发基础106-Instruments16.iOS开发基础105-Xcode收集Crashs的各种方法17.iOS开发基础104-正向代理和反向代理18.iOS开发基础103-APP之间跳转19.iOS开发基础102-后台保活方案
20.iOS开发基础101-指纹和面部识别
21.iOS开发基础100-MDM证书申请流程22.iOS开发基础99-iOS 内购的防范与优化23.iOS开发基础98-跳转淘宝案例24.iOS开发基础97-应用内购(In-App Purchase)的安全性解析与收据处理流程25.iOS开发基础96-UI类继承关系图26.iOS开发基础95-程序内评价27.iOS开发基础94-xcode1028.iOS开发基础93-GCD死锁29.iOS开发基础92-线程保活30.iOS开发基础91-线程同步技术与资源共享详解31.iOS开发基础138-视频编码32.iOS开发基础137-音视频编解码简介33.iOS开发基础136-防暴力点击34.iOS开发基础135-Core Data35.iOS开发基础134-异步并行上传问题36.iOS开发基础133-崩溃预防37.iOS开发基础132-POSIX线程库38.iOS开发基础131-isa指针39.iOS开发基础130-视频录制上传40.iOS开发基础129-音频录制上传41.iOS开发基础128-应用本地化42.iOS开发基础127-深入探讨KVO43.iOS开发基础126-深入探索设计模式44.iOS开发基础125-深入探索SDWebImage45.iOS开发基础124-RunLoop实现卡顿检测46.iOS开发基础123-自动释放池原理47.iOS开发基础122-RunLoop48.iOS开发基础121-APP启动优化49.iOS开发基础120-通知与线程50.iOS开发基础119-组件化51.iOS开发基础118-Runtime52.iOS开发基础117-Hybrid53.iOS开发基础116-性能监控54.iOS开发基础115-Socket55.iOS开发基础114-YYCache56.iOS开发基础113-Unity3D57.iOS开发基础112-GCD常见场景58.iOS开发基础111-RAC59.iOS开发基础110-Core Graphics应用场景60.iOS开发基础109-网络安全61.iOS开发基础108-常见的编程范式62.iOS开发基础107-iOS直播63.iOS开发基础148-ABM vs MDM64.iOS开发基础147-ABM集中管理Apple设备65.iOS开发基础146-深入解析WKWebView66.iOS开发基础145-Apple Search Ads67.iOS开发基础144-逐字打印效果68.iOS开发基础143-性能优化69.iOS开发基础142-广告归因70.iOS开发基础141-音频解码71.iOS开发基础140-音频编码72.iOS开发基础139-视频解码73.iOS开发基础149-由UUIDString引发的思考在iOS开发中,使用FaceID和TouchID可以为用户提供安全的生物识别认证,而手势识别(Gesture Recognition)可以增加用户交互的便利性和灵活性。下面将详细介绍这三种技术,并给出如何封装一个统一的工具类来供外部使用。
一、FaceID与TouchID
1. 设置与配置
在使用FaceID和TouchID之前,需要在项目的Info.plist中添加授权描述。
<key>NSFaceIDUsageDescription</key>
<string>我们需要使用Face ID来验证你的身份</string>
<key>NSFaceIDUsageDescription</key>
<string>我需要使用Face ID来验证用户身份</string>
<key>NSLocalAuthenticationUsageDescription</key>
<string>我们需要使用登录来授权使用应用程序</string>
2. FaceID与TouchID的使用
使用LocalAuthentication框架来进行生物识别认证。
import LocalAuthentication
class BiometricAuthenticator {
let context = LAContext()
var error: NSError?
func authenticateUser(completion: @escaping (String?) -> Void) {
let reason = "请验证您的身份,以继续使用我们的服务。"
// 检查设备是否支持生物识别
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let biometricType = context.biometryType == .faceID ? "FaceID" : "TouchID"
//进行生物识别认证
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, error in
DispatchQueue.main.async {
if success {
completion("\(biometricType)认证成功")
} else {
completion("认证失败: \(error?.localizedDescription)")
}
}
}
} else {
completion("生物识别不可用: \(error?.localizedDescription)")
}
}
}
二、手势识别
1. 配置手势识别
手势识别通常通过UIGestureRecognizer
及其子类来实现。
import UIKit
class GestureRecognizerHelper {
func addTapGesture(to view: UIView, target: Any, action: Selector) {
let tap = UITapGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(tap)
}
func addSwipeGesture(to view: UIView, target: Any, action: Selector, direction: UISwipeGestureRecognizer.Direction) {
let swipe = UISwipeGestureRecognizer(target: target, action: action)
swipe.direction = direction
view.addGestureRecognizer(swipe)
}
func addPinchGesture(to view: UIView, target: Any, action: Selector) {
let pinch = UIPinchGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(pinch)
}
func addRotationGesture(to view: UIView, target: Any, action: Selector) {
let rotation = UIRotationGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(rotation)
}
}
三、封装统一工具类
将FaceID、TouchID和手势识别封装到一个工具类中,便于外部使用。
import LocalAuthentication
import UIKit
class AuthenticationAndGestureHelper {
// MARK: - FaceID & TouchID
private let context = LAContext()
private var error: NSError?
func authenticateUser(completion: @escaping (String?) -> Void) {
let reason = "请验证您的身份,以继续使用我们的服务。"
// 检查设备是否支持生物识别
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let biometricType = context.biometryType == .faceID ? "FaceID" : "TouchID"
// 进行生物识别认证
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, error in
DispatchQueue.main.async {
if success {
completion("\(biometricType)认证成功")
} else {
completion("认证失败: \(error?.localizedDescription)")
}
}
}
} else {
completion("生物识别不可用: \(error?.localizedDescription)")
}
}
// MARK: - Gesture Recognition
func addTapGesture(to view: UIView, target: Any, action: Selector) {
let tap = UITapGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(tap)
}
func addSwipeGesture(to view: UIView, target: Any, action: Selector, direction: UISwipeGestureRecognizer.Direction) {
let swipe = UISwipeGestureRecognizer(target: target, action: action)
swipe.direction = direction
view.addGestureRecognizer(swipe)
}
func addPinchGesture(to view: UIView, target: Any, action: Selector) {
let pinch = UIPinchGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(pinch)
}
func addRotationGesture(to view: UIView, target: Any, action: Selector) {
let rotation = UIRotationGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(rotation)
}
}
四、使用示例
import UIKit
class ViewController: UIViewController {
let authHelper = AuthenticationAndGestureHelper()
override func viewDidLoad() {
super.viewDidLoad()
// 使用面部识别或者指纹识别
authHelper.authenticateUser { message in
if let message = message {
print(message)
}
}
// 添加手势
authHelper.addTapGesture(to: self.view, target: self, action: #selector(handleTapGesture))
authHelper.addSwipeGesture(to: self.view, target: self, action: #selector(handleSwipeGesture), direction: .right)
}
@objc func handleTapGesture() {
print("Tap Gesture Recognized")
}
@objc func handleSwipeGesture() {
print("Swipe Gesture Recognized")
}
}
对应的oc版本
一、FaceID与TouchID
1. 设置与配置
在使用FaceID和TouchID之前,需要在项目的Info.plist中添加授权描述。
<key>NSFaceIDUsageDescription</key>
<string>我们需要使用Face ID来验证你的身份</string>
<key>NSLocalAuthenticationUsageDescription</key>
<string>我们需要使用登录来授权使用应用程序</string>
2. FaceID与TouchID的使用
使用LocalAuthentication框架来进行生物识别认证。
#import <LocalAuthentication/LocalAuthentication.h>
#import <UIKit/UIKit.h>
@interface BiometricAuthenticator : NSObject
- (void)authenticateUserWithCompletion:(void (^)(NSString *))completion;
@end
@implementation BiometricAuthenticator
- (void)authenticateUserWithCompletion:(void (^)(NSString *))completion {
LAContext *context = [[LAContext alloc] init];
NSError *error = nil;
NSString *reason = @"请验证您的身份,以继续使用我们的服务。";
// 检查设备是否支持生物识别
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
NSString *biometricType = context.biometryType == LABiometryTypeFaceID ? @"FaceID" : @"TouchID";
// 进行生物识别认证
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:reason reply:^(BOOL success, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (success) {
completion([NSString stringWithFormat:@"%@认证成功", biometricType]);
} else {
completion([NSString stringWithFormat:@"认证失败: %@", error.localizedDescription]);
}
});
}];
} else {
completion([NSString stringWithFormat:@"生物识别不可用: %@", error.localizedDescription]);
}
}
@end
二、手势识别
1. 配置手势识别
手势识别通常通过UIGestureRecognizer
及其子类来实现。
#import <UIKit/UIKit.h>
@interface GestureRecognizerHelper : NSObject
- (void)addTapGestureToView:(UIView *)view target:(id)target action:(SEL)action;
- (void)addSwipeGestureToView:(UIView *)view target:(id)target action:(SEL)action direction:(UISwipeGestureRecognizerDirection)direction;
- (void)addPinchGestureToView:(UIView *)view target:(id)target action:(SEL)action;
- (void)addRotationGestureToView:(UIView *)view target:(id)target action:(SEL)action;
@end
@implementation GestureRecognizerHelper
- (void)addTapGestureToView:(UIView *)view target:(id)target action:(SEL)action {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
[view addGestureRecognizer:tap];
}
- (void)addSwipeGestureToView:(UIView *)view target:(id)target action:(SEL)action direction:(UISwipeGestureRecognizerDirection)direction {
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:target action:action];
swipe.direction = direction;
[view addGestureRecognizer:swipe];
}
- (void)addPinchGestureToView:(UIView *)view target:(id)target action:(SEL)action {
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:target action:action];
[view addGestureRecognizer:pinch];
}
- (void)addRotationGestureToView:(UIView *)view target:(id)target action:(SEL)action {
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:target action:action];
[view addGestureRecognizer:rotation];
}
@end
三、封装统一工具类
将FaceID、TouchID和手势识别封装到一个工具类中,便于外部使用。
#import <LocalAuthentication/LocalAuthentication.h>
#import <UIKit/UIKit.h>
@interface AuthenticationAndGestureHelper : NSObject
// FaceID & TouchID
- (void)authenticateUserWithCompletion:(void (^)(NSString *message))completion;
// Gesture Recognition
- (void)addTapGestureToView:(UIView *)view target:(id)target action:(SEL)action;
- (void)addSwipeGestureToView:(UIView *)view target:(id)target action:(SEL)action direction:(UISwipeGestureRecognizerDirection)direction;
- (void)addPinchGestureToView:(UIView *)view target:(id)target action:(SEL)action;
- (void)addRotationGestureToView:(UIView *)view target:(id)target action:(SEL)action;
@end
@implementation AuthenticationAndGestureHelper {
LAContext *_context;
NSError *_error;
}
- (instancetype)init {
self = [super init];
if (self) {
_context = [[LAContext alloc] init];
}
return self;
}
- (void)authenticateUserWithCompletion:(void (^)(NSString *message))completion {
NSString *reason = @"请验证您的身份,以继续使用我们的服务。";
if ([_context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&_error]) {
NSString *biometricType = _context.biometryType == LABiometryTypeFaceID ? @"FaceID" : @"TouchID";
[_context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:reason reply:^(BOOL success, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (success) {
completion([NSString stringWithFormat:@"%@认证成功", biometricType]);
} else {
completion([NSString stringWithFormat:@"认证失败: %@", error.localizedDescription]);
}
});
}];
} else {
completion([NSString stringWithFormat:@"生物识别不可用: %@", _error.localizedDescription]);
}
}
- (void)addTapGestureToView:(UIView *)view target:(id)target action:(SEL)action {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
[view addGestureRecognizer:tap];
}
- (void)addSwipeGestureToView:(UIView *)view target:(id)target action:(SEL)action direction:(UISwipeGestureRecognizerDirection)direction {
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:target action:action];
swipe.direction = direction;
[view addGestureRecognizer:swipe];
}
- (void)addPinchGestureToView:(UIView *)view target:(id)target action:(SEL)action {
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:target action:action];
[view addGestureRecognizer:pinch];
}
- (void)addRotationGestureToView:(UIView *)view target:(id)target action:(SEL)action {
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:target action:action];
[view addGestureRecognizer:rotation];
}
@end
四、使用示例
#import "ViewController.h"
#import "AuthenticationAndGestureHelper.h"
@interface ViewController ()
@property (nonatomic, strong) AuthenticationAndGestureHelper *authHelper;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.authHelper = [[AuthenticationAndGestureHelper alloc] init];
// 使用面部识别或者指纹识别
[self.authHelper authenticateUserWithCompletion:^(NSString *message) {
if (message) {
NSLog(@"%@", message);
}
}];
// 添加手势
[self.authHelper addTapGestureToView:self.view target:self action:@selector(handleTapGesture)];
[self.authHelper addSwipeGestureToView:self.view target:self action:@selector(handleSwipeGesture) direction:UISwipeGestureRecognizerDirectionRight];
}
- (void)handleTapGesture {
NSLog(@"Tap Gesture Recognized");
}
- (void)handleSwipeGesture {
NSLog(@"Swipe Gesture Recognized");
}
@end
通过上述代码示例,我们实现了对FaceID、TouchID和手势识别的封装,并展示了如何在实际项目中进行调用和使用。这样可以大大提高代码的复用性和可维护性。
将来的你会感谢今天如此努力的你!
版权声明:本文为博主原创文章,未经博主允许不得转载。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
2015-07-16 iOS开发基础8-UIScrollView