From d6c2dc318ca1df5dec1beef8cbfc9dda5f766aa6 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Mon, 13 Mar 2017 14:25:19 -0400 Subject: [PATCH] Remove direct use of UIApplication from GTLR. In addition to the preprocessor check to remove the code, this shifts to remove the direct class reference so it is easier to build once and use the code in an extension or an app. --- Source/Objects/GTLRService.m | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Source/Objects/GTLRService.m b/Source/Objects/GTLRService.m index be2147a85..f9f3113a8 100644 --- a/Source/Objects/GTLRService.m +++ b/Source/Objects/GTLRService.m @@ -2578,7 +2578,22 @@ - (void)cancelTicket { id app = [GTMSessionFetcher substituteUIApplication]; if (app) return app; - return (id)[UIApplication sharedApplication]; + static Class applicationClass = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + BOOL isAppExtension = [[[NSBundle mainBundle] bundlePath] hasSuffix:@".appex"]; + if (!isAppExtension) { + Class cls = NSClassFromString(@"UIApplication"); + if (cls && [cls respondsToSelector:NSSelectorFromString(@"sharedApplication")]) { + applicationClass = cls; + } + } + }); + + if (applicationClass) { + app = (id)[applicationClass sharedApplication]; + } + return app; } #endif // GTM_BACKGROUND_TASK_FETCHING