Skip to content

Commit

Permalink
Remove direct use of UIApplication from GTMSessionFetcher.
Browse files Browse the repository at this point in the history
iOS application extensions don't play well with +[UIApplication sharedApplication],
and attempting to compile the symbol gets a compile error. We previously used
GTM_BACKGROUND_UIAPPLICATION to gate access, but that doesn't work when building
shared frameworks to use with both the app and an extension.

This removes the GTM_BACKGROUND_UIAPPLICATION define altogether. It does leave
GTM_BACKGROUND_TASK_FETCHING, in case clients want to compile out the background
task behavior altogether.
  • Loading branch information
thomasvl committed Mar 6, 2017
1 parent a742bdd commit 2c8f5ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
7 changes: 0 additions & 7 deletions Source/GTMSessionFetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,6 @@
#define GTM_BACKGROUND_TASK_FETCHING 1
#endif

// If GTM_BACKGROUND_TASK_FETCHING is enabled and GTMUIApplicationProtocol is not used,
// GTM_BACKGROUND_UIAPPLICATION will allow defaulting to UIApplication. To avoid references to
// UIApplication (e.g. for extensions), set GTM_BACKGROUND_UIAPPLICATION to 0.
#if TARGET_OS_IPHONE && !TARGET_OS_WATCH && !defined(GTM_BACKGROUND_UIAPPLICATION)
#define GTM_BACKGROUND_UIAPPLICATION 1
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
26 changes: 19 additions & 7 deletions Source/GTMSessionFetcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -1977,13 +1977,25 @@ + (void)setSubstituteUIApplication:(nullable id<GTMUIApplicationProtocol>)app {
id<GTMUIApplicationProtocol> app = gSubstituteUIApp;
if (app) return app;

// Some projects use GTM_BACKGROUND_UIAPPLICATION to avoid compile-time references
// to UIApplication.
#if GTM_BACKGROUND_UIAPPLICATION
return (id<GTMUIApplicationProtocol>) [UIApplication sharedApplication];
#else
return nil;
#endif
// iOS App extensions should not call [UIApplication sharedApplication], even
// if UIApplication responds to it.

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 // TARGET_OS_IPHONE

Expand Down

0 comments on commit 2c8f5ab

Please sign in to comment.