Search code examples
iostouch-id

Touch ID Crashing App


When trying to use TouchID on my app, it hangs for a long time, and then will sometimes crash, and sometimes finally pass. I have never tried anything with Touch ID, so it's all very much new to me. My code is:

-(IBAction)touchID {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *student = [defaults objectForKey:@"firstName"];
    NSString *usingTouch = [defaults objectForKey:@"useTouchID"];

    if ([usingTouch isEqualToString:@"Yes"]) {
        [passwordfield removeFromSuperview];
        LAContext *context = [[LAContext alloc] init];

        NSError *error = nil;
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                    localizedReason:@"Are you the device owner?"
                              reply:^(BOOL success, NSError *error) {

                                  if (error) {
                                      NSLog(@"%@", error);
                                      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                      message:@"There was a problem verifying your identity."
                                                                                     delegate:nil
                                                                            cancelButtonTitle:@"Ok"
                                                                            otherButtonTitles:nil];
                                      [alert show];
                                      return;
                                  }

                                  else if (success) {
                                      NSString *nope = @"Yes";
                                      [defaults setObject:nope forKey:@"firstName"];
                                      [defaults synchronize];
                                      [self updatetheview];

                                  } else {
                                      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                      message:@"You are not the device owner."
                                                                                     delegate:nil
                                                                            cancelButtonTitle:@"Ok"
                                                                            otherButtonTitles:nil];
                                      [alert show];
                                  }

                              }];

        } else {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"Your device cannot authenticate using TouchID."
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
            [alert show];

        }
        //Perform Touch ID Code
    }

}

The Console shows this over and over again before finally acting as you would expect it to immediately:

 Stack:(
    0   CoreFoundation                      0x000000018359cf60  + 148
    1   libobjc.A.dylib                     0x0000000198a47f80 objc_exception_throw + 56
    2   CoreFoundation                      0x000000018359ce90  + 0
    3   Foundation                          0x00000001845bb2d8  + 88
    4   Foundation                          0x0000000184441a1c  + 56
    5   Foundation                          0x000000018443d5dc  + 260
    6   UIKit                               0x0000000188c378c4  + 64
    7   UIKit                               0x0000000188b2aa78  + 240
    8   UIKit                               0x0000000188b2a32c  + 116
    9   UIKit                               0x0000000188b2a1b8  + 504
    10  UIKit                               0x0000000188e3c834  + 228
    11  UIKit                               0x0000000188b28bf8  + 412
    12  Fritch                              0x0000000100186f40 -[DirectoryViewController updatetheview] + 180
    13  Fritch                              0x0000000100186cac __34-[DirectoryViewController touchID]_block_invoke + 316
    14  LocalAuthentication                 0x000000018599ee28  + 72
    15  LocalAuthentication                 0x000000018599e7e4  + 256
    16  LocalAuthentication                 0x000000018599b004  + 324
    17  LocalAuthentication                 0x000000018599b168  + 128
    18  CoreFoundation                      0x00000001835a2a70  + 144
    19  CoreFoundation                      0x00000001834a04d4  + 284
    20  Foundation                          0x00000001845c5b4c  + 20
    21  Foundation                          0x00000001845c5940  + 796
    22  Foundation                          0x00000001845c79c8  + 292
    23  libxpc.dylib                        0x00000001994a0c80  + 28
    24  libxpc.dylib                        0x00000001994a0c24  + 40
    25  libdispatch.dylib                   0x000000010051dc68 _dispatch_client_callout + 16
    26  libdispatch.dylib                   0x000000010052a780 _dispatch_queue_drain + 1036
    27  libdispatch.dylib                   0x0000000100521958 _dispatch_queue_invoke + 464
    28  libdispatch.dylib                   0x000000010051dc68 _dispatch_client_callout + 16
    29  libdispatch.dylib                   0x000000010052cec8 _dispatch_root_queue_drain + 2344
    30  libdispatch.dylib                   0x000000010052c590 _dispatch_worker_thread3 + 132
    31  libsystem_pthread.dylib             0x000000019946d470 _pthread_wqthread + 1092
    32  libsystem_pthread.dylib             0x000000019946d020 start_wqthread + 4
)

Solution

  • The reply block of evaluatePolicy is not called on the main thread. You need to make sure all of your UI code in the reply block is done on the main thread.