iOS 9使代码顺序(Touch ID)

我是一名开发人员,使用自学的Objective-C编写iPhone代码。 我们正在使用Xcode 7和iOS 9.我的任务是为我的公司创build一个应用程序,以便在有人到达时进行通信,并按照为他们制定的日程安排(使用XML)进行预约。

我们希望使用Touch ID来保证安全,因此用户不必login很多。 由于应用程序中数据的机密性,安全性是必需的。 我有应用程序工作。 或者,它看起来像它的工作,直到我点击Touch ID中的取消选项,并意识到Touch ID是没有提供安全。

我相信这是因为在Touch ID之后应该运行的方法在LA上下文块之外。 我把它们放在外面,因为我需要在成功的授权(通过XML检索到的内部员工号码)上返回一个string值。 如果我把返回的NSString的方法放在LA Context块之外,这些方法的运行顺序是错误的。

我怎样才能让以下4种方法顺序运行? 1)requireLogin(Touch ID code here)2)requestSession 3)readSession 4)checkSession

如果Touch ID失败,则方法2-4不应运行。

我看过块。 我看了GCD。 我找不到如何处理这个问题的例子。 如果有人能指引我正确的方向。 我真的很感激。 如果我需要包括更多的代码,请让我知道。

先谢谢你。

- (NSString *)retrieveESN { NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0]; NSString *fullPath = [documentsDirectoryPath stringByAppendingString:@"/session.txt"]; if ([fileManager fileExistsAtPath:fullPath]==YES) { _finished = false; NSLog(@"File exists"); [self readSession]; NSLog(@"%@", _sessionDetail); NSLog(@"Pre Check Session"); NSString *result = [self checkSession:_sessionDetail]; NSLog(@"Post Check Session"); NSLog(@"%@", result); if ([result isEqualToString:@"Error"]) { NSLog(@"Error received - going into newFile"); NSString *userESN = [self newFile]; NSLog(@"Error'd returning with result"); NSLog(@"UserESN: %@", userESN); return userESN; } else { NSLog(@"No Error Received"); NSLog(@"Returning result: %@", result); return result; } } else { _finished = false; NSLog(@"No file exists, call newFile"); NSString *userESN = [self newFile]; NSLog(@"UserESN: %@", userESN); return userESN; } } - (NSString *)newFile { NSLog(@"File does not exist."); [self requireLogin]; NSLog(@"Out of requireLogin"); [self requestSession]; NSLog(@"Out of requestSession"); [self readSession]; NSLog(@"Out of readSession"); NSString *userESN = [self checkSession:_sessionDetail]; NSLog(@"Into checkSession again newfile"); return userESN; } - (void)requireLogin { LAContext *context = [[LAContext alloc] init]; NSError *error = nil; NSLog(@"Into requireLogin"); if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { // Authenticate User [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"You must log in to the app." reply:^(BOOL success, NSError * _Nullable error) { if (success) { _finished = true; NSLog(@"success"); } else { switch (error.code) { case LAErrorAuthenticationFailed: NSLog(@"Authentication Failed"); break; case LAErrorUserCancel: NSLog(@"User pressed Cancel button"); break; case LAErrorUserFallback: //Return to go to Enter Password not available screen. NSLog(@"User pressed Enter Password"); break; default: NSLog(@"Touch ID is not configured"); break; } NSLog(@"Authentication Fails"); } }]; } else { NSLog(@"Phone doesn't support Touch ID."); } } - (void)requestSession { NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; NSString *url = @“https://companyurl.com/session_page”; NSLog(@"Got to requestSession"); NSMutableString *postText = [[NSMutableString alloc] init]; NSLog(@"%@", idfv); [postText appendString:idfv]; NSString *postBody = [NSString stringWithString:postText]; XMLPostSecurity *postAction = [[XMLPostSecurity alloc] init]; NSLog(@"Got to PostAction"); _sessionDetail = [postAction sendPostRequestToUrl:url withBody:postBody]; NSLog(@"Session Detail: %@", _sessionDetail); FileSaving *saver = [[FileSaving alloc] init]; [saver saveSession:self.sessionDetail]; NSLog(@"Saved file."); } -(NSString *)readSession { NSLog(@"In to readSession."); NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0]; NSString *fullPath = [documentsDirectoryPath stringByAppendingString:@"/session.txt"]; while ([fileManager fileExistsAtPath:fullPath]==NO); NSFileManager *fileManagerTwo; NSData *dataBuffer; fileManagerTwo = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingString:@"/session.txt"]; NSLog(@"Got Path"); dataBuffer = [fileManagerTwo contentsAtPath:filePath]; NSLog(@"databuffer created"); _sessionDetail = [[NSString alloc] initWithData:dataBuffer encoding:(NSASCIIStringEncoding)]; NSLog(@"File read: %@", _sessionDetail); return _sessionDetail; } -(NSString *)checkSession:(NSString *)sessionFound { NSLog(@"Into checkSession"); NSDictionary *cookieProperties = [NSDictionary dictionaryWithObjectsAndKeys: @"ollie/", NSHTTPCookieDomain, @"\\", NSHTTPCookiePath, @"cookieName", NSHTTPCookieName, sessionFound, NSHTTPCookieValue, nil]; NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties]; NSArray *cookieArray = [NSArray arrayWithObject:cookie]; NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray]; NSMutableString *url = [[NSMutableString alloc] initWithString:@“https://companyurl.com/xmlfile.php”]; NSLog(@"%@", url); NSURL *urlNew = [NSURL URLWithString:url]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlNew]; [request setHTTPMethod:@"GET"]; [request setAllHTTPHeaderFields:headers]; self.parseData = NULL; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (error) { NSLog(@"dataTaskWithRequest error: %@", error); return; } if ([response isKindOfClass:[NSHTTPURLResponse class]]) { NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; if (statusCode != 200) { NSLog(@"dataTaskWithRequest HTTP status code: %ld", (long)statusCode); if (statusCode == 401) { // Insert process for thumbprint and session cookie pull NSLog(@"401 Error received"); NSFileManager *fileManagerThree = [NSFileManager defaultManager]; NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *sessionPath = [documentsPath stringByAppendingPathComponent:@"session.txt"]; NSError *error; BOOL success = [fileManagerThree removeItemAtPath:sessionPath error:&error]; if (success) { NSLog(@"File deleted - check session."); } else { NSLog(@"File could not be deleted."); } } else { NSLog(@"I got something other than 200 or 401"); return; } } } self.parseData = data; }]; [task resume]; while ( self.parseData == NULL ); ParseTypeXML *myParser = [[ParseTypeXML alloc] initWithData:self.parseData]; if ([myParser.esn count] == 0) { return @"Error"; } else { return myParser.esn[0]; } }