Skip to content

Commit

Permalink
Remove direct use of UIApplication from GTLR.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thomasvl committed Mar 13, 2017
1 parent 6a7dca0 commit d6c2dc3
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Source/Objects/GTLRService.m
Original file line number Diff line number Diff line change
Expand Up @@ -2578,7 +2578,22 @@ - (void)cancelTicket {
id<GTMUIApplicationProtocol> app = [GTMSessionFetcher substituteUIApplication];
if (app) return app;

return (id<GTMUIApplicationProtocol>)[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<GTMUIApplicationProtocol>)[applicationClass sharedApplication];
}
return app;
}
#endif // GTM_BACKGROUND_TASK_FETCHING

Expand Down

0 comments on commit d6c2dc3

Please sign in to comment.