From 5e1b3a3ef95b8654185804e98a279dddf2653fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Spie=C3=9F?= Date: Thu, 1 Oct 2015 15:00:37 +0200 Subject: [PATCH] Run UI code in authenticator completions on main queue --- Classes/BITAuthenticationViewController.m | 47 +++++---- Classes/BITAuthenticator.m | 116 ++++++++++++---------- 2 files changed, 86 insertions(+), 77 deletions(-) diff --git a/Classes/BITAuthenticationViewController.m b/Classes/BITAuthenticationViewController.m index addbfd1815..687ab947c5 100644 --- a/Classes/BITAuthenticationViewController.m +++ b/Classes/BITAuthenticationViewController.m @@ -93,7 +93,7 @@ [self updateBarButtons]; [self updateWebLoginButton]; } - } + } } - (void) updateWebLoginButton { @@ -279,7 +279,7 @@ [self saveAction:nil]; } } - return NO; + return NO; } #pragma mark - Actions @@ -294,24 +294,26 @@ if(succeeded) { //controller should dismiss us shortly.. } else { - /* We won't use this for now until we have a more robust solution for displaying UIAlertController - // requires iOS 8 - id uialertcontrollerClass = NSClassFromString(@"UIAlertController"); - if (uialertcontrollerClass) { - UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil - message:error.localizedDescription - preferredStyle:UIAlertControllerStyleAlert]; + dispatch_async(dispatch_get_main_queue(), ^{ - - UIAlertAction *okAction = [UIAlertAction actionWithTitle:BITHockeyLocalizedString(@"OK") - style:UIAlertActionStyleCancel - handler:^(UIAlertAction * action) {}]; - - [alertController addAction:okAction]; - - [self presentViewController:alertController animated:YES completion:nil]; - } else { - */ + /* We won't use this for now until we have a more robust solution for displaying UIAlertController + // requires iOS 8 + id uialertcontrollerClass = NSClassFromString(@"UIAlertController"); + if (uialertcontrollerClass) { + UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil + message:error.localizedDescription + preferredStyle:UIAlertControllerStyleAlert]; + + + UIAlertAction *okAction = [UIAlertAction actionWithTitle:BITHockeyLocalizedString(@"OK") + style:UIAlertActionStyleCancel + handler:^(UIAlertAction * action) {}]; + + [alertController addAction:okAction]; + + [self presentViewController:alertController animated:YES completion:nil]; + } else { + */ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil @@ -321,9 +323,10 @@ otherButtonTitles:nil]; [alertView show]; #pragma clang diagnostic pop - /*}*/ - typeof(self) strongSelf = weakSelf; - [strongSelf setLoginUIEnabled:YES]; + /*}*/ + typeof(self) strongSelf = weakSelf; + [strongSelf setLoginUIEnabled:YES]; + }); } }]; } diff --git a/Classes/BITAuthenticator.m b/Classes/BITAuthenticator.m index 753bd51465..68932a0c60 100644 --- a/Classes/BITAuthenticator.m +++ b/Classes/BITAuthenticator.m @@ -89,9 +89,11 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; } #pragma mark - +/** + * This method has to be called on the main queue + */ - (void)dismissAuthenticationControllerAnimated:(BOOL)animated completion:(void (^)(void))completion { if (!_authenticationController) return; - UIViewController *presentingViewController = [_authenticationController presentingViewController]; // If there is no presenting view controller just remove view @@ -163,7 +165,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) { return; } - + NSLog(@"[HockeySDK] ERROR: The authentication token could not be stored due to a keychain error. This is most likely a signing or keychain entitlement issue!"); } @@ -251,33 +253,34 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; - (void) validate { [self validateWithCompletion:^(BOOL validated, NSError *error) { - if(validated) { - [self dismissAuthenticationControllerAnimated:YES completion:nil]; - } else { - BITHockeyLog(@"Validation failed with error: %@", error); - /* We won't use this for now until we have a more robust solution for displaying UIAlertController - // requires iOS 8 - id uialertcontrollerClass = NSClassFromString(@"UIAlertController"); - if (uialertcontrollerClass) { - __weak typeof(self) weakSelf = self; - - UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil - message:error.localizedDescription - preferredStyle:UIAlertControllerStyleAlert]; - - - UIAlertAction *okAction = [UIAlertAction actionWithTitle:BITHockeyLocalizedString(@"HockeyOK") - style:UIAlertActionStyleDefault - handler:^(UIAlertAction * action) { - typeof(self) strongSelf = weakSelf; - [strongSelf validate]; - }]; - - [alertController addAction:okAction]; - - [self showAlertController:alertController]; + dispatch_async(dispatch_get_main_queue(), ^{ + if(validated) { + [self dismissAuthenticationControllerAnimated:YES completion:nil]; } else { - */ + BITHockeyLog(@"Validation failed with error: %@", error); + /* We won't use this for now until we have a more robust solution for displaying UIAlertController + // requires iOS 8 + id uialertcontrollerClass = NSClassFromString(@"UIAlertController"); + if (uialertcontrollerClass) { + __weak typeof(self) weakSelf = self; + + UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil + message:error.localizedDescription + preferredStyle:UIAlertControllerStyleAlert]; + + + UIAlertAction *okAction = [UIAlertAction actionWithTitle:BITHockeyLocalizedString(@"HockeyOK") + style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) { + typeof(self) strongSelf = weakSelf; + [strongSelf validate]; + }]; + + [alertController addAction:okAction]; + + [self showAlertController:alertController]; + } else { + */ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil @@ -288,8 +291,9 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; [alertView setTag:0]; [alertView show]; #pragma clang diagnostic pop - /*}*/ - } + /*}*/ + } + }); }]; } @@ -361,7 +365,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; } } -- (void)handleValidationResponseWithData:(NSData *) responseData error:(NSError *)error completion:(void (^)(BOOL validated, NSError *))completion { +- (void)handleValidationResponseWithData:(NSData *)responseData error:(NSError *)error completion:(void (^)(BOOL validated, NSError *))completion { if(nil == responseData) { NSDictionary *userInfo = @{NSLocalizedDescriptionKey : BITHockeyLocalizedString(@"HockeyAuthenticationFailedAuthenticate")}; if(error) { @@ -373,7 +377,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; code:BITAuthenticatorNetworkError userInfo:userInfo]; self.validated = NO; - if(completion) completion(NO, error); + if(completion) { completion(NO, error); } } else { NSError *validationParseError = nil; BOOL valid = [self.class isValidationResponseValid:responseData error:&validationParseError]; @@ -381,11 +385,11 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; if(valid) { [self setLastAuthenticatedVersion:self.executableUUID]; } - if(completion) completion(valid, validationParseError); + if(completion) { completion(valid, validationParseError); } } } -- (NSDictionary*) validationParameters { +- (NSDictionary*)validationParameters { NSParameterAssert(self.installationIdentifier); NSParameterAssert(self.installationIdentifierParameterString); @@ -397,7 +401,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; return @{self.installationIdentifierParameterString : self.installationIdentifier}; } -+ (BOOL) isValidationResponseValid:(id) response error:(NSError **) error { ++ (BOOL)isValidationResponseValid:(id)response error:(NSError **)error { NSParameterAssert(response); NSError *jsonParseError = nil; @@ -457,7 +461,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; NSParameterAssert(self.identificationType == BITAuthenticatorIdentificationTypeHockeyAppEmail || (password && password.length)); NSURLRequest* request = [self requestForAuthenticationEmail:email password:password]; - + id nsurlsessionClass = NSClassFromString(@"NSURLSessionUploadTask"); BOOL isURLSessionSupported = (nsurlsessionClass && !bit_isRunningInAppExtension()); @@ -467,7 +471,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; - (void)authenticationViewController:(UIViewController *)viewController handleAuthenticationWithEmail:(NSString *)email request:(NSURLRequest *)request - urlSessionSupported:(BOOL)isURLSessionSupported + urlSessionSupported:(BOOL)isURLSessionSupported completion:(void (^)(BOOL, NSError *))completion { __weak typeof (self) weakSelf = self; if(isURLSessionSupported) { @@ -494,8 +498,8 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; - (void)handleAuthenticationWithResponse:(NSHTTPURLResponse *)response email:(NSString *)email data:(NSData *)data completion:(void (^)(BOOL, NSError *))completion{ NSError *authParseError = nil; NSString *authToken = [self.class authenticationTokenFromURLResponse:response - data:data - error:&authParseError]; + data:data + error:&authParseError]; BOOL identified; if(authToken) { identified = YES; @@ -510,9 +514,11 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; identified = NO; } self.identified = identified; - completion(identified, authParseError); - if(self.identificationCompletion) self.identificationCompletion(identified, authParseError); - self.identificationCompletion = nil; + if (completion) { completion(identified, authParseError); } + if(self.identificationCompletion) { + self.identificationCompletion(identified, authParseError); + self.identificationCompletion = nil; + } } - (NSURLRequest *) requestForAuthenticationEmail:(NSString*) email password:(NSString*) password { @@ -523,7 +529,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; if (installString) { params[@"install_string"] = installString; } - + if(BITAuthenticatorIdentificationTypeHockeyAppEmail == self.identificationType) { NSString *authCode = BITHockeyMD5([NSString stringWithFormat:@"%@%@", self.authenticationSecret ? : @"", @@ -798,7 +804,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; free(source); return; } - + if ((fs.st_size < 20) || (memcmp(source, kBITPNGHeader, 8))) { // Not a PNG free(source); @@ -806,7 +812,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; } buffer = source + 8; - + NSString *result = nil; bit_uint32 length; unsigned char *name; @@ -827,7 +833,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; if (bytes_left >= length) { memcpy(data, buffer, length); - + buffer += length; buffer += 4; if (!strcmp((const char *)name, "tEXt")) { @@ -843,7 +849,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; chunk_index = 128; } } - + free(data); free(name); @@ -874,12 +880,12 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; } if(nil == _appDidEnterBackgroundObserver) { _appDidEnterBackgroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification - object:nil - queue:NSOperationQueue.mainQueue - usingBlock:^(NSNotification *note) { - typeof(self) strongSelf = weakSelf; - [strongSelf applicationDidEnterBackground:note]; - }]; + object:nil + queue:NSOperationQueue.mainQueue + usingBlock:^(NSNotification *note) { + typeof(self) strongSelf = weakSelf; + [strongSelf applicationDidEnterBackground:note]; + }]; } } @@ -901,10 +907,10 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; [self removeKeyFromKeychain:kBITAuthenticatorIdentifierTypeKey]; } else { BOOL success1 = [self addStringValueToKeychainForThisDeviceOnly:installationIdentifier - forKey:kBITAuthenticatorIdentifierKey]; + forKey:kBITAuthenticatorIdentifierKey]; NSParameterAssert(success1); BOOL success2 = [self addStringValueToKeychainForThisDeviceOnly:[self.class stringForIdentificationType:type] - forKey:kBITAuthenticatorIdentifierTypeKey]; + forKey:kBITAuthenticatorIdentifierTypeKey]; NSParameterAssert(success2); if (!success1 || !success2) { [self alertOnFailureStoringTokenInKeychain];