Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为更干净的代码重构本地身份验证框架_Ios_Objective C_Refactoring - Fatal编程技术网

Ios 为更干净的代码重构本地身份验证框架

Ios 为更干净的代码重构本地身份验证框架,ios,objective-c,refactoring,Ios,Objective C,Refactoring,通过将该方法添加到ViewController,我成功地在代码中实现了本地身份验证框架,但我希望重构代码,以便更有效地采用MVC模式。我想将代码移动到NSObject中并单独调用它,但我遇到了两个问题 作为参考,以下是我正在实施的代码: 第一个问题是,当我用UIButton触摸测试代码时,代码没有触发,第二个问题是,我想在成功时调用PerformsgueWithIdentifier,但我需要UIViewController来调用它。这就是我最初尝试的: Authenticate.h @

通过将该方法添加到ViewController,我成功地在代码中实现了本地身份验证框架,但我希望重构代码,以便更有效地采用MVC模式。我想将代码移动到NSObject中并单独调用它,但我遇到了两个问题

作为参考,以下是我正在实施的代码:

第一个问题是,当我用UIButton触摸测试代码时,代码没有触发,第二个问题是,我想在成功时调用PerformsgueWithIdentifier,但我需要UIViewController来调用它。这就是我最初尝试的:

Authenticate.h     
@interface Authenticate : NSObject
- (void)startAuthenticating;

Authenticate.m 
@implementation Authenticate
- (void)startAuthenticating{
    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;
    ...                          
    if (success) {
      [self performSegueWithIdentifier: @"Success" sender:nil];
    ...
    } ...   
  }
^[self-Performsgue…]会在这里抛出一个错误,我理解

#import "Authenticate.h"
ViewController.h
@interface ViewController : UIViewController
@property(nonatomic, strong) Authenticate *touchID;

ViewController.m
@implementation ViewController
- (void)viewDidLoad{
    ...
    [self.touchID startAuthenticating];
  }    
我也试着
#将“ViewController.h”导入Authenticate.h并添加
@property(非原子,弱)ViewController*ViewController
而不是
[自我执行模式…]
它变成了
[self.viewController performsgue…]
即使这看起来像一个保留周期


目前,我已经通过将本地身份验证框架实现到另一个UIViewController中来重构我的代码,并将我的主ViewController子类化到该AuthenticationViewController下,但这对我来说就像是一个骗局,因为这不是我的目标。有人能告诉我如何更好地完成这项任务吗?

我不知道为什么你的代码没有启动。但对于第二个问题,请执行以下操作:

在Authenticate类中,将方法编写为

- (void)startAuthenticatingWithCompletion:(void (^)(BOOL success, NSError *error))completion{
    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;

    if (success) {
        completion(YES, nil);
        //[self performSegueWithIdentifier: @"Success" sender:nil];

    } else {
        completion(NO, authError);
    }
}
在ViewController类中调用如下方法:

[self.touchID startAuthenticatingWithCompletion:^(BOOL success, NSError *error) {
        if (success) {
            [self performSegue...];
        }
    }]; 

我不知道为什么你的密码没有被激活。但对于第二个问题,请执行以下操作:

在Authenticate类中,将方法编写为

- (void)startAuthenticatingWithCompletion:(void (^)(BOOL success, NSError *error))completion{
    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;

    if (success) {
        completion(YES, nil);
        //[self performSegueWithIdentifier: @"Success" sender:nil];

    } else {
        completion(NO, authError);
    }
}
在ViewController类中调用如下方法:

[self.touchID startAuthenticatingWithCompletion:^(BOOL success, NSError *error) {
        if (success) {
            [self performSegue...];
        }
    }];