Skip to content

Commit d6c2dc3

Browse files
committed
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.
1 parent 6a7dca0 commit d6c2dc3

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Source/Objects/GTLRService.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2578,7 +2578,22 @@ - (void)cancelTicket {
25782578
id<GTMUIApplicationProtocol> app = [GTMSessionFetcher substituteUIApplication];
25792579
if (app) return app;
25802580

2581-
return (id<GTMUIApplicationProtocol>)[UIApplication sharedApplication];
2581+
static Class applicationClass = nil;
2582+
static dispatch_once_t onceToken;
2583+
dispatch_once(&onceToken, ^{
2584+
BOOL isAppExtension = [[[NSBundle mainBundle] bundlePath] hasSuffix:@".appex"];
2585+
if (!isAppExtension) {
2586+
Class cls = NSClassFromString(@"UIApplication");
2587+
if (cls && [cls respondsToSelector:NSSelectorFromString(@"sharedApplication")]) {
2588+
applicationClass = cls;
2589+
}
2590+
}
2591+
});
2592+
2593+
if (applicationClass) {
2594+
app = (id<GTMUIApplicationProtocol>)[applicationClass sharedApplication];
2595+
}
2596+
return app;
25822597
}
25832598
#endif // GTM_BACKGROUND_TASK_FETCHING
25842599

0 commit comments

Comments
 (0)