Skip to content

Commit a630e1b

Browse files
authored
Merge pull request #102 from thomasvl/background_task_tweaks
Remove direct use of UIApplication from GTLR.
2 parents ac844a2 + d6c2dc3 commit a630e1b

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)