你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開發之TouchId

iOS開發之TouchId

編輯:IOS開發綜合

// 需求: 經過touchId停止登錄,我把賬號與密碼運用keychain存儲了

LoginViewModel

// 這裡只是登錄懇求的例子

- (void)requestLoginStart {
// 這裡停止網絡懇求,登錄成功,前往後果
if(succ) {
// 運用keychain存儲登錄密碼
[PassWordTool savePassWord:self.password];
}

引入 #import “PassWordTool.h”

LoginVC.m


- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
if ([user objectForKey:@"isRemeberID"]) { // 這個isRemeberID 能否記住賬號
    self.phoneNumberTextField.text = [user objectForKey:@"remberID"];
    //  調用
    [self touchIDManager:^(BOOL succ, NSString *error) {
        if (succ) {

            self.loginViewModel.password = self.pass;

            [[self.loginViewModel requestLoginStart] subscribeNext:^(id x) {
            //  假如登錄成功,去到主界面
                return [UIApplication sharedApplication].cutToMainWindow;
            }];
        }
    }];
}

}

-(void)touchIDManager:(void(^)(BOOL succ,NSString * error))block{

NSDictionary * dic = [self arrayForLoaclForMobile:[self.phoneNumberTextField.text removeAllSpaceForString]];
if (dic.allValues.count && [dic[@"Touch_STATUS"] isEqualToString:@"1"]) {
    [self createTouchForHanderBlock:^(BOOL success, NSInteger errCode, NSString *error) {
        if (success) {

            [self touchIDLoginCtrl];
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user setBool:success forKey:@"Touch_hander"];
            [user synchronize];

            block(success,nil);
        }else{
            [self.view makeToast:error duration:2.0f position:@"center"];
        }
    }];
}

}
-(void)touchIDLoginCtrl{
NSDictionary * dic = [self arrayForLoaclForMobile:[self.phoneNumberTextField.text removeAllSpaceForString]];
self.mob = dic[@”Touch_MOBILE”];
self.pass = dic[@”Touch_Pass”];
}

// 引入#import

UIViewController+BaseCategory.m


#define dispatch_main_async__safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_async(dispatch_get_main_queue(), block);\
}

@implementation UIViewController (DHBaseCategory)

// touchIdAction
-(void)createTouchForHanderBlock:(void(^)(BOOL success, NSInteger errCode, NSString * error))reply{

LAContext *context = [[LAContext alloc]init];
context.localizedFallbackTitle = @"輸出密碼";
NSError *error = nil;
if ([context canEvaLuatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
    [context evaLuatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
            localizedReason:@"請驗證您的指紋"
                      reply:^(BOOL success, NSError * _Nullable error) {
                          if(success){

                              dispatch_main_async__safe(^{
                                  reply(success,0,nil);
                              });

                          }else{

                              NSString * errorString = nil;
                              switch (error.code) {
                                  case LAErrorAuthenticationFailed:
                                      errorString = @"用戶身份驗證失敗";
                                      break;

                                  case LAErrorUserCancel:
                                      errorString = @"您已取消";
                                      break;

                                  case LAErrorPasscodeNotSet:
                                      errorString = @"密碼在設備上沒有設置";
                                      break;

                                  case LAErrorTouchIDNotAvailable:
                                      errorString = @"touid在設備上不可用";
                                      break;

                                  case LAErrorTouchIDNotEnrolled:
                                      errorString = @"沒有注銷的手指在觸摸";
                                      break;

                                  default:
                                      errorString = @"請稍候重試";
                                      break;

                              }
                              dispatch_main_async__safe(^{
                                  reply(success,error.code,errorString);
                              });

                          }


                      }];
}else{
    dispatch_main_async__safe(^{
        reply(NO,-100,@"您的設備不支持TouchID");
    });
}

}

//touchID 開關

-(void)updateDataSourceAndControlForSave:(BOOL)isSave AndPwd:(NSString *)pwd{

NSDictionary * dic;
if (isSave) {
    //  這裡的 Touch_STATUS  是touchid的形態
    dic = @{
            @"Touch_MOBILE" : [DHAccountModel shareAccount].custLogin,
            @"Touch_Pass"  : pwd,
            @"Touch_STATUS" : @"1" 
            };

}else{

    dic = @{
            @"Touch_MOBILE" : [DHAccountModel shareAccount].custLogin,
            @"Touch_Pass"  : @"",
            @"Touch_STATUS" : @"0"
            };
}

[self setTouchIDStatusForLocal:dic];

}

-(void)setTouchIDStatusForLocal:(NSDictionary *)dic{

NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];

NSArray * array =  [userDefault objectForKey:K_TOUCHID_LOCALSAVE_KEY];
NSMutableArray * mutaleArray  = [NSMutableArray arrayWithArray:array];
//從本地讀取mobile
NSDictionary * resultDic = [self arrayForLoaclForMobile:[DHAccountModel shareAccount].custLogin];
if (resultDic) {
    [mutaleArray removeObject:resultDic];
}
[mutaleArray addObject:dic];

[userDefault setObject:[mutaleArray copy] forKey:K_TOUCHID_LOCALSAVE_KEY];
[userDefault synchronize];

}

-(NSDictionary )arrayForLoaclForMobile:(NSString )mobile{

NSMutableArray * mutaleArray = [[self getTouchIDStatusForLocal] mutableCopy];

__block NSDictionary * resultDic = nil;
[mutaleArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

    NSDictionary * dic = (NSDictionary *)obj;
    if ([dic[@"Touch_MOBILE"] isEqualToString:mobile]) {
        resultDic = dic;
    }
}];
return resultDic;

}
-(NSArray *)getTouchIDStatusForLocal{
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
return [userDefault objectForKey:K_TOUCHID_LOCALSAVE_KEY];
}

【iOS開發之TouchId】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved