{"id":20900070,"url":"https://github.com/lennonchin/lcauthmanager","last_synced_at":"2025-05-13T01:32:07.933Z","repository":{"id":56918195,"uuid":"150585160","full_name":"LennonChin/LCAuthManager","owner":"LennonChin","description":"一个简单、高效、易用的权限验证库，包括手势密码、Touch ID和Face ID；A comprehensive, efficient and easy-to-use rights verification library, including Gesture Password, Touch ID and Face ID.","archived":false,"fork":false,"pushed_at":"2018-09-29T06:55:03.000Z","size":4173,"stargazers_count":31,"open_issues_count":2,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-30T02:02:29.072Z","etag":null,"topics":["auth","authentication","faceid","gesturelock","ios","iphone","iphonex","iphonexs","iphonexsmax","touchid"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LennonChin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-27T12:43:01.000Z","updated_at":"2024-12-25T08:16:12.000Z","dependencies_parsed_at":"2022-08-20T20:50:39.649Z","dependency_job_id":null,"html_url":"https://github.com/LennonChin/LCAuthManager","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LennonChin%2FLCAuthManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LennonChin%2FLCAuthManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LennonChin%2FLCAuthManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LennonChin%2FLCAuthManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LennonChin","download_url":"https://codeload.github.com/LennonChin/LCAuthManager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253854026,"owners_count":21974213,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","authentication","faceid","gesturelock","ios","iphone","iphonex","iphonexs","iphonexsmax","touchid"],"created_at":"2024-11-18T11:17:55.797Z","updated_at":"2025-05-13T01:32:07.336Z","avatar_url":"https://github.com/LennonChin.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LCAuthManager简介\n\nLCAuthManager是一个简单易用的权限验证库，提供了手势密码、生物特性验证（包括Touch ID和Face ID）的集成。\n\n![ProjectLogo](https://github.com/LennonChin/LCAuthManager/blob/master/LCAuthManager/Images/ProjectLogo.jpg)\n\nLCAuthManager有以下优点：\n\n- 可配置性强，提供了针对手势密码页面及特性的配置，例如密码长度、试错最大次数、辅助操作等。\n- 可结合使用，可以将手势密码和生物特性验证结合在一起，由开发者自定义进行配置。\n- 提供大量对外接口，提供了凭证存储的对外接口，开发者可以接入自己的凭证存储方式。\n- 项目提供了常见的Demo，可以协助开发者快速集成。\n\n# 使用效果\n\n1. 使用Face ID + 手势密码的效果：\n\n![Face ID with Gesture Password Demo](https://github.com/LennonChin/LCAuthManager/blob/master/LCAuthManager/Images/FaceIDDemo.gif)\n\n2. 使用Touch ID + 手势密码的效果：\n\n![Touch ID with Gesture Password Demo](https://github.com/LennonChin/LCAuthManager/blob/master/LCAuthManager/Images/TouchIDDemo.gif)\n\n# 快速上手\n\n- 使用场景一：使用FaceID或TouchID进行验证\n\n\u003e 注意：使用FaceID时需要在`info.plist`文件中添加`NSFaceIDUsageDescription`权限申请说明，否则无法使用FaceID，系统会直接Crash。配置例子如下：\n\n```xml\n\u003ckey\u003eNSFaceIDUsageDescription\u003c/key\u003e\n\u003cstring\u003eApp需要您的同意，才能访问您的面容识别功能，用于安全验证\u003c/string\u003e\n```\n\n然后使用下面的代码进行验证：\n\n```objective-c\n#import \"LCAuthManager.h\"\n...\n\nif ([LCAuthManager isBiometricsAuthOpened:LCBiometricsTypeUnknown]) { // 检查生物特性识别是否可用\n\t// 自定义提示信息\n\tNSString *reason = (([LCBiometricsAuthManager isSupportBiometricsAuth] == LCBiometricsTypeTouchID) ? @\"按压您的指纹以解锁\" : @\"验证您的面容以解锁\");\n\t// 开始验证生物特性识别，系统会自动判断是使用TouchID还是使用FaceID\n\t[LCAuthManager verifyBiometricsAuthWithReason:reason fallbackTitle:@\"\" Success:^(LCBiometricsAuthCheckResultType checkResultType) {\n\t\tdispatch_async(dispatch_get_main_queue(), ^{\n\t\t\t// 处理验证通过的场景\n\t\t});\n\t} Fail:^(LCBiometricsAuthCheckResultType checkResultType, NSError *error) {\n\t\tdispatch_async(dispatch_get_main_queue(), ^{\n\t\t\t// 处理验证未通过的场景\n\t\t});\n\t} Fallback:^(LCBiometricsAuthCheckResultType checkResultType, NSError *error) {\n\t\tdispatch_async(dispatch_get_main_queue(), ^{\n\t\t\t// 处理当用户多次验证未通过，点击了系统弹出框右侧按钮的事件\n\t\t});\n\t} delegate:self];\n}\n```\n\n- 使用场景二：创建手势密码\n\n```objective-c\n#import \"LCAuthManager.h\"\n#import \"LCAuthManagerConfig.h\"\n...\n\n// 打开创建手势密码页面\nUIWindow *window = [UIApplication sharedApplication].keyWindow;\nif(window.rootViewController.presentingViewController == nil){\n\t[LCAuthManager globalConfig].useAssistOperations = NO;\n\t[LCAuthManager showGestureAuthViewControllerWithType:LCGestureAuthViewTypeCreate hostViewControllerView:self delegate:self];\n}\n\n// 处理结果的代理方法\n- (void)gestureCheckState:(LCGestureAuthCheckResultType)checkResultType viewType:(LCGestureAuthViewType)viewType {\n    if (viewType == LCGestureAuthViewTypeCreate) {\n        if (checkResultType == LCGestureAuthViewCheckResultSuccess) {\n            [self showAlert:@\"创建手势密码成功\"];\n        } else if (checkResultType == LCGestureAuthViewCheckResultFailed) {\n            [self showAlert:@\"创建手势密码失败\"];\n        } else if (checkResultType == LCGestureAuthViewCheckResultCancel) {\n\n            [self showAlert:@\"取消创建手势密码\"];\n        }\n\n    }\n}\n```\n\n- 使用场景三：使用手势密码验证\n\n```objective-c\n#import \"LCAuthManager.h\"\n...\n\n// 手势密码\nLCGestureAuthViewController *gestureAuthViewController = nil;\nif ([LCAuthManager isGestureAuthOpened]) {\n\tUINavigationController *rootViewController = (UINavigationController *)self.window.rootViewController;\n\tgestureAuthViewController = [LCAuthManager showGestureAuthViewControllerWithType:LCGestureAuthViewTypeCheck hostViewControllerView:rootViewController.topViewController delegate:self];\n}\n```\n\n# 详细使用\n\n1. 引入本项目\n\n- 使用CocoaPods；LCAuthManager已支持CocoaPods导入，请在你的Podfile中添加以下内容并更新项目依赖：\n\n```\npod 'LCAuthManager'\n```\n\n- 或者可以选择手动引入。克隆本项目到本地，将项目主目录下的LCAuthManager文件夹拖入工程，在需要使用的地方引入`#import \"LCAuthManager.h`头文件即可。\n\n2. 了解项目配置和代理：\n\n- 项目枚举\n\n项目中使用的枚举统一位于`LCAuthManagerConstant.h`文件中，开发过程中可以参考。\n\n- 全局配置\n\nLCAuthManager的配置都交由LCAuthManagerConfig类处理，并且配置是全局唯一的，所有的配置项全都可以在该类中找到；可以使用`[LCAuthManager globalConfig]`获取到全局LCAuthManagerConfig对象，然后直接修改配置项即可，如：\n\n```objective-c\n// 配置手势密码页面的背景图\n[LCAuthManager globalConfig].gestrueVCBackgroundImage = [UIImage imageNamed:@\"background\"];\n// 配置手势密码的最小长度\n[LCAuthManager globalConfig].gesturePasswordMinLength = 5;\n// 配置手势密码最大试错次数\n[LCAuthManager globalConfig].maxGestureRetryTimes = 3;\n// 配置使用手势密码时是否使用生物特性识别辅助\n[LCAuthManager globalConfig].useBiometricsAuthAssist = YES;\n```\n\n\u003e 注：需要注意的是，由于配置是全局唯一，所以前一次的配置会影响到后一次的功能，如果每次使用需求不同则每次使用都应该单独配置。\n\n- 代理\n\n关于功能上的某些回调，大部分使用了代理向外暴露接口，将操作委托给用户处理；本项目的对外代理都位于LCAuthManager类的中LCAuthManagerDelegate中，如下：\n\n```objective-c\n@protocol LCAuthManagerDelegate \u003cNSObject\u003e\n\n@optional\n#pragma mark - 手势密码相关\n/** 手势密码相关，针对某种验证的验证结果 */\n- (void)gestureCheckState:(LCGestureAuthCheckResultType)checkResultType viewType:(LCGestureAuthViewType)viewType;\n/** 手势密码相关，达到最大次数代理方法 */\n- (void)gestureRetryReachMaxTimesWithAuthController:(LCGestureAuthViewController *)gestureAuthViewController viewType:(LCGestureAuthViewType)viewType;\n/** 辅助操作相关，operationType：0-第一个辅助按钮被点击，1-第二个辅助按钮被点击 */\n- (void)assistOperationWithAuthController:(LCGestureAuthViewController *)gestureAuthViewController viewType:(LCGestureAuthViewType)viewType operationType:(NSInteger)operationType;\n\n#pragma mark - 生物特性识别相关\n/** 生物特性识别相关，针对某种验证的验证结果 */\n- (void)biometricsCheckState:(LCBiometricsAuthCheckResultType)checkResultType biometricsType:(LCBiometricsType)biometricsType error:(LCBiometricsCheckError *)error;\n\n#pragma mark - 密码持久化相关\n/** 查看是否设置了生物特性识别 */\n- (BOOL)isBiometricsAuthOpened:(LCBiometricsType)biometricsType;\n/** 生物特性识别相关，持久化生物特性识别的开启状态 */\n- (BOOL)persistBiometricsAuth:(LCBiometricsType)biometricsType isOn:(BOOL)isOn;\n/** 查看是否设置了手势密码 */\n- (BOOL)isGestureAuthOpened;\n/** 手势密码相关，持久化手势密码 */\n- (BOOL)persistGestureAuth:(NSString *)password;\n/** 加载手势密码 */\n- (NSString *)loadGestureAuth;\n@end\n```\n\n当然针对某些便捷操作也提供了相应的Block简化流程；当代理和Block同时设置时，相应的委托操作将同时有效。\n\n3. 使用生物特性识别\n\n\u003e 注意：使用FaceID时需要在`info.plist`文件中添加`NSFaceIDUsageDescription`权限申请说明，否则无法使用FaceID，系统会直接Crash。\n\niOS中生物特性识别包括TouchID和FaceID，这两种验证方式对外暴露的接口非常简单，仅仅只提供了验证逻辑，也就是说，用户在使用这两类验证时，**只能是调用验证方法，获得验证结果**这两种操作。在LCAuthManager中对TouchID和FaceID进行了封装，外界不需要关心设备是否支持生物特性识别或设备支持哪种生物特性识别方法，只需要使用统一的方法进行调用即可。LCAuthManager中提供了对生物特性识别相关的支持方法：\n\n```objective-c\n/** 是否支持生物特性识别 */\n+ (LCBiometricsType)isSupportBiometricsAuth;\n/** 查看是否设置了生物特性识别 */\n+ (BOOL)isBiometricsAuthOpened:(LCBiometricsType)biometricsType;\n/** 生物特性识别相关，持久化生物特性识别的开启状态 */\n+ (BOOL)persistBiometricsAuth:(LCBiometricsType)biometricsType isOn:(BOOL)isOn;\n```\n\n其中第一个方法可以用于判断设备是否支持生物特性识别及支持生物特性识别的类型；第二和第三个方法提供了对生物特性识别凭证的持久化存储和读取，默认情况下凭证是存储在用户偏好设置（`[NSUserDefaults standardUserDefaults]`）中以`LCAuthManagerConfig`为键的字典中的，开发者也可以自定义自己的凭证存储方式，这一点在后面进行讨论。\n\n对生物特性识别的调用可以直接使用LCAuthManager类下面的方法：\n\n```objective-c\n/**\n 使用生物识别验证\n\n @param reason 显示的理由\n @param fallbackTitle 验证出错后的右边按钮\n @param successBlock 成功回调Block\n @param failBlock 失败回调Block\n @param fallbackBlock 当生物识别达到最大次数时，会出现弹框，点击右侧按钮会调用该Block\n */\n+ (void)verifyBiometricsAuthWithReason:(NSString *)reason\n                         fallbackTitle:(NSString *)fallbackTitle\n                               Success:(void (^)(LCBiometricsAuthCheckResultType checkResultType))successBlock\n                                  Fail:(void (^)(LCBiometricsAuthCheckResultType checkResultType, NSError *error))failBlock\n                              Fallback:(void (^)(LCBiometricsAuthCheckResultType checkResultType, NSError *error))fallbackBlock\n                              delegate:(id\u003cLCAuthManagerDelegate\u003e)delegate;\n```\n\n该方法有三个Block可以处理三种不同的验证结果，同时还可以传入代理，让代理来处理验证结果。\n\n4. 使用手势密码\n\n手势密码提供下面的几类操作：\n\n```objective-c\ntypedef NS_ENUM(NSUInteger, LCGestureAuthViewType) {\n    LCGestureAuthViewTypeCheck,     // 检查手势密码\n    LCGestureAuthViewTypeCreate,    // 创建手势密码\n    LCGestureAuthViewTypeModify,    // 修改\n    LCGestureAuthViewTypeClean,     // 清除\n    LCGestureAuthViewTypeUnknown,   // 其他未知\n};\n```\n\n所有的操作都通过LCAuthManager类的一个方法进行：\n\n```objective-c\n+ (LCGestureAuthViewController *)showGestureAuthViewControllerWithType:(LCGestureAuthViewType)lockViewType hostViewControllerView:(UIViewController *)hostViewController delegate:(id\u003cLCAuthManagerDelegate\u003e)delegate;\n```\n\n该方法要求传入操作类型、`hostViewControllerView`和代理，其中操作类型用于加载不同的手势识别页面和处理逻辑，`hostViewControllerView`用于以present的方式展示手势密码控制器，代理用于处理验证业务中的各类结果和委托事件，相关的代理事件方法可以查阅前一个小节。\n\n5. 限时忽略验证\n\n在某些场景中，我们可能需要暂时忽略权限的验证，比如当我们从App内分享内容到社交平台后，大部分用户在分享完成后会立即返回App，此时如果也需要权限的验证将会降低使用连贯性，因此LCAuthManager提供了临时忽略验证的方法：\n\n```objective-c\n/**\n 暂时关闭验证\n 适用于如对外分享内容后返回App，此时应该暂时不用验证\n\n @param timeout 限时时间，单位：秒\n */\n+ (void)setCloseAuthTemporary:(NSTimeInterval)timeout;\n```\n\n例如我们可以在AppDelegate的`- (void)applicationDidEnterBackground:(UIApplication *)application`方法中设置忽略限时时间，如下面的例子设置了15秒的限时忽略：\n\n```objective-c\n- (void)applicationDidEnterBackground:(UIApplication *)application {\n    /**\n     * 设置不验证的限时时间\n     * 设置这个时间之后，用户切出App，并在这个时间内切回不会有验证操作\n     * 使用场景：比如分享内容到其他App后切回本App，时间间隔很短，可以临时关闭验证\n     */\n    [LCAuthManager setCloseAuthTemporary:15];\n}\n```\n\n那么当App进入后台后，在15秒的时间内重新切回App是不会有权限验证的。\n\n# 凭证的存储和读取\n\n手势密码及生物特性识别虽然提供给开发者一种便捷的权限验证方法，但权限验证的凭证存储依旧是需要面对的问题。作为开发者来说，我并不推荐大家使用这些功能来保护重要的私密数据。**在本项目中对凭证也只是简单地做了本地的存储（存放在用户偏好设置中）。当然如果你想要使用这些功能来保护重要的数据，建议使用本项目提供的接口，自己处理凭证的存储，例如存储在钥匙串中。**\n\n\u003e 注意：本项目默认对凭证的存储处理是直接存放在用户偏好设置中，其中手势密码是明文存储的数字序列，生物特性识别是简单存储的开启或关闭的标识。相关代码可以查看LCAuthPasswordManager类和LCBiometricsAuthManager类。\n\n在LCAuthManagerConfig的配置中有一项`keepAuthentication`的配置，该配置项默认为`NO`，如果你想要自己处理凭证的存储和读取，可以将该配置置为`YES`，然后实现LCAuthManagerDelegate指定的下列方法手动处理凭证的存储和读取：\n\n```objective-c\n#pragma mark - 密码持久化相关\n/** 查看是否设置了生物识别 */\n- (BOOL)isBiometricsAuthOpened:(LCBiometricsType)biometricsType;\n/** 生物识别相关，持久化生物识别的开启状态 */\n- (BOOL)persistBiometricsAuth:(LCBiometricsType)biometricsType isOn:(BOOL)isOn;\n/** 查看是否设置了手势密码 */\n- (BOOL)isGestureAuthOpened;\n/** 手势密码相关，持久化手势密码 */\n- (BOOL)persistGestureAuth:(NSString *)password;\n/** 加载手势密码 */\n- (NSString *)loadGestureAuth;\n```\n\n这些方法都是与用户无关的，开发者可以自定义绑定凭证信息到用户标识，只需要保证凭证在存储和读取时的一致性即可。\n\n\u003e 注意：如果你配置了`keepAuthentication`为`YES`，但没有实现上述的任意一个方法，运行时将会出现断言错误。\n\n# License\n\n[MIT](https://opensource.org/licenses/mit)\n\nCopyright (c) 2018-present, LennonChin\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flennonchin%2Flcauthmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flennonchin%2Flcauthmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flennonchin%2Flcauthmanager/lists"}