From 05de1686faf7f02a2190da74cb17599d80bccea9 Mon Sep 17 00:00:00 2001 From: Andreas Linde Date: Mon, 24 Aug 2015 17:01:58 +0200 Subject: [PATCH] Fix alert presentation with UIAlertController --- Classes/BITAuthenticator.m | 2 +- Classes/BITCrashManager.m | 2 +- Classes/BITHockeyBaseManager.m | 54 ++++++++++++++++++++++----- Classes/BITHockeyBaseManagerPrivate.h | 1 + Classes/BITStoreUpdateManager.m | 2 +- Classes/BITUpdateManager.m | 12 +++--- 6 files changed, 54 insertions(+), 19 deletions(-) diff --git a/Classes/BITAuthenticator.m b/Classes/BITAuthenticator.m index 3669e7baff..cecfcaed6f 100644 --- a/Classes/BITAuthenticator.m +++ b/Classes/BITAuthenticator.m @@ -275,7 +275,7 @@ static unsigned char kBITPNGEndChunk[4] = {0x49, 0x45, 0x4e, 0x44}; [alertController addAction:okAction]; - [self showView:alertController]; + [self showAlertController:alertController]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 60da0f3b37..5e2bf4bfd3 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -1091,7 +1091,7 @@ static void uncaught_cxx_exception_handler(const BITCrashUncaughtCXXExceptionInf [alertController addAction:alwaysSendAction]; } - [self showView:alertController]; + [self showAlertController:alertController]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" diff --git a/Classes/BITHockeyBaseManager.m b/Classes/BITHockeyBaseManager.m index 8c7879f59c..f01d667d85 100644 --- a/Classes/BITHockeyBaseManager.m +++ b/Classes/BITHockeyBaseManager.m @@ -201,12 +201,12 @@ return navController; } -- (void)showView:(UIViewController *)viewController { +- (UIViewController *)visibleWindowRootViewController { // if we compile Crash only, then BITHockeyBaseViewController is not included // in the headers and will cause a warning with the modulemap file #if HOCKEYSDK_FEATURE_AUTHENTICATOR || HOCKEYSDK_FEATURE_UPDATES || HOCKEYSDK_FEATURE_FEEDBACK UIViewController *parentViewController = nil; - + if ([[BITHockeyManager sharedHockeyManager].delegate respondsToSelector:@selector(viewControllerForHockeyManager:componentManager:)]) { parentViewController = [[BITHockeyManager sharedHockeyManager].delegate viewControllerForHockeyManager:[BITHockeyManager sharedHockeyManager] componentManager:self]; } @@ -222,14 +222,6 @@ parentViewController = parentViewController.presentedViewController; } - // as per documentation this only works if called from within viewWillAppear: or viewDidAppear: - // in tests this also worked fine on iOS 6 and 7 but not on iOS 5 so we are still trying this - if ([parentViewController isBeingPresented]) { - BITHockeyLog(@"WARNING: There is already a view controller being presented onto the parentViewController. Delaying presenting the new view controller by 0.5s."); - [self performSelector:@selector(showView:) withObject:viewController afterDelay:0.5]; - return; - } - // special addition to get rootViewController from three20 which has it's own controller handling if (NSClassFromString(@"TTNavigator")) { #pragma clang diagnostic push @@ -241,6 +233,46 @@ #pragma clang diagnostic pop } + return parentViewController; +#else + return nil; +#endif +} + +- (void)showAlertController:(UIViewController *)alertController { + // if we compile Crash only, then BITHockeyBaseViewController is not included + // in the headers and will cause a warning with the modulemap file +#if HOCKEYSDK_FEATURE_AUTHENTICATOR || HOCKEYSDK_FEATURE_UPDATES || HOCKEYSDK_FEATURE_FEEDBACK + UIViewController *parentViewController = [self visibleWindowRootViewController]; + + // as per documentation this only works if called from within viewWillAppear: or viewDidAppear: + // in tests this also worked fine on iOS 6 and 7 but not on iOS 5 so we are still trying this + if ([parentViewController isBeingPresented]) { + BITHockeyLog(@"WARNING: There is already a view controller being presented onto the parentViewController. Delaying presenting the new view controller by 0.5s."); + [self performSelector:@selector(showAlertController:) withObject:alertController afterDelay:0.5]; + return; + } + + if (parentViewController) { + [parentViewController presentViewController:alertController animated:YES completion:nil]; + } +#endif +} + +- (void)showView:(UIViewController *)viewController { + // if we compile Crash only, then BITHockeyBaseViewController is not included + // in the headers and will cause a warning with the modulemap file +#if HOCKEYSDK_FEATURE_AUTHENTICATOR || HOCKEYSDK_FEATURE_UPDATES || HOCKEYSDK_FEATURE_FEEDBACK + UIViewController *parentViewController = [self visibleWindowRootViewController]; + + // as per documentation this only works if called from within viewWillAppear: or viewDidAppear: + // in tests this also worked fine on iOS 6 and 7 but not on iOS 5 so we are still trying this + if ([parentViewController isBeingPresented]) { + BITHockeyLog(@"WARNING: There is already a view controller being presented onto the parentViewController. Delaying presenting the new view controller by 0.5s."); + [self performSelector:@selector(showView:) withObject:viewController afterDelay:0.5]; + return; + } + if (_navController != nil) _navController = nil; _navController = [self customNavigationControllerWithRootViewController:viewController presentationStyle:_modalPresentationStyle]; @@ -260,6 +292,8 @@ } else { // if not, we add a subview to the window. A bit hacky but should work in most circumstances. // Also, we don't get a nice animation for free, but hey, this is for beta not production users ;) + UIWindow *visibleWindow = [self findVisibleWindow]; + BITHockeyLog(@"INFO: No rootViewController found, using UIWindow-approach: %@", visibleWindow); if ([viewController isKindOfClass:[BITHockeyBaseViewController class]]) [(BITHockeyBaseViewController *)viewController setModalAnimated:NO]; diff --git a/Classes/BITHockeyBaseManagerPrivate.h b/Classes/BITHockeyBaseManagerPrivate.h index d4fba69baf..de87acd769 100644 --- a/Classes/BITHockeyBaseManagerPrivate.h +++ b/Classes/BITHockeyBaseManagerPrivate.h @@ -71,6 +71,7 @@ // UI helpers - (UIWindow *)findVisibleWindow; - (UINavigationController *)customNavigationControllerWithRootViewController:(UIViewController *)viewController presentationStyle:(UIModalPresentationStyle)presentationStyle; +- (void)showAlertController:(UIViewController *)alertController; - (void)showView:(UIViewController *)viewController; #endif diff --git a/Classes/BITStoreUpdateManager.m b/Classes/BITStoreUpdateManager.m index b4740c447d..ce7da15c40 100644 --- a/Classes/BITStoreUpdateManager.m +++ b/Classes/BITStoreUpdateManager.m @@ -454,7 +454,7 @@ [alertController addAction:showAction]; - [self showView:alertController]; + [self showAlertController:alertController]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" diff --git a/Classes/BITUpdateManager.m b/Classes/BITUpdateManager.m index 456395d35d..4e9a5ce1ba 100644 --- a/Classes/BITUpdateManager.m +++ b/Classes/BITUpdateManager.m @@ -103,7 +103,7 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) { [alertController addAction:okAction]; - [self showView:alertController]; + [self showAlertController:alertController]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -576,7 +576,7 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) { [alertController addAction:installAction]; - [self showView:alertController]; + [self showAlertController:alertController]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -640,7 +640,7 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) { [alertController addAction:installAction]; } - [self showView:alertController]; + [self showAlertController:alertController ]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -759,7 +759,7 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) { [alertController addAction:checkAction]; } - [self showView:alertController]; + [self showAlertController:alertController]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -903,7 +903,7 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) { [alertController addAction:okAction]; - [self showView:alertController]; + [self showAlertController:alertController]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -1115,7 +1115,7 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) { [alertController addAction:okAction]; - [self showView:alertController]; + [self showAlertController:alertController]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations"