Skip to content

Commit

Permalink
Fix scenedelegate support
Browse files Browse the repository at this point in the history
Summary:
When SDK initializes, connectedScenes may be an empty Set, we should check info.plist (info.plist defines scenedelegates) instead.

https://developer.apple.com/documentation/uikit/app_and_environment/scenes/specifying_the_scenes_your_app_supports?language=objc

Differential Revision: D43100903

fbshipit-source-id: 0f06c477d18c4b80e6ec1dab0ab611299f8eff09
  • Loading branch information
KylinChang authored and facebook-github-bot committed Feb 7, 2023
1 parent 49e49f7 commit 4509d5a
Showing 1 changed file with 54 additions and 24 deletions.
78 changes: 54 additions & 24 deletions FBSDKCoreKit/FBSDKCoreKit/AppEvents/Internal/AEM/FBSDKAEMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,32 +95,62 @@ - (void)setup
NSSet<UIScene *> *scenes = [[UIApplication sharedApplication] connectedScenes];
for (UIScene* thisScene in scenes) {
Class sceneClass = thisScene.delegate.class;
[self.swizzler swizzleSelector:@selector(scene:openURLContexts:) onClass:sceneClass withBlock:^(id sceneDelegate, SEL cmd, id scene, NSSet<UIOpenURLContext *> *urlContexts) {
[self.aemReporter enable];
for(UIOpenURLContext* urlContext in urlContexts) {
[self.aemReporter handle:urlContext.URL];
[self.appEventsUtility saveCampaignIDs:urlContext.URL];
}
[self logAutoSetupStatus:YES source:@"scenedelegate_dl"];
} named:@"AEMSceneDeeplinkAutoSetup"];

[self.swizzler swizzleSelector:@selector(scene:continueUserActivity:) onClass:sceneClass withBlock:^(id sceneDelegate, SEL cmd, id scene, NSUserActivity *userActivity) {
[self.aemReporter enable];
[self.aemReporter handle:userActivity.webpageURL];
[self.appEventsUtility saveCampaignIDs:userActivity.webpageURL];
[self logAutoSetupStatus:YES source:@"scenedelegate_ul"];
} named:@"AEMSceneUniversallinkAutoSetup"];

[self.swizzler swizzleSelector:@selector(scene:willConnectToSession:options:) onClass:sceneClass withBlock:^(id sceneDelegate, SEL cmd, id scene, UISceneSession *session, UISceneConnectionOptions *options) {
[self.aemReporter enable];
for(UIOpenURLContext* urlContext in options.URLContexts) {
[self.aemReporter handle:urlContext.URL];
[self.appEventsUtility saveCampaignIDs:urlContext.URL];
}
[self logAutoSetupStatus:YES source:@"scenedelegate_coldstart"];
} named:@"AEMSceneColdStartAutoSetup"];
[self setupScene:sceneClass];
}
NSSet<NSString *> *sceneDelegates = [self getSceneDelegates];
for (NSString *sceneDelegate in sceneDelegates) {
[self setupScene:NSClassFromString(sceneDelegate)];
}
}
}

- (void)setupScene:(Class)sceneClass
{
if (sceneClass == nil) {
return;
}
if (@available(iOS 13.0, *)) {
[self.swizzler swizzleSelector:@selector(scene:openURLContexts:) onClass:sceneClass withBlock:^(id sceneDelegate, SEL cmd, id scene, NSSet<UIOpenURLContext *> *urlContexts) {
[self.aemReporter enable];
for(UIOpenURLContext* urlContext in urlContexts) {
[self.aemReporter handle:urlContext.URL];
[self.appEventsUtility saveCampaignIDs:urlContext.URL];
}
[self logAutoSetupStatus:YES source:@"scenedelegate_dl"];
} named:[NSString stringWithFormat:@"AEMSceneDeeplinkAutoSetup_%@", NSStringFromClass(sceneClass)]];

[self.swizzler swizzleSelector:@selector(scene:continueUserActivity:) onClass:sceneClass withBlock:^(id sceneDelegate, SEL cmd, id scene, NSUserActivity *userActivity) {
[self.aemReporter enable];
[self.aemReporter handle:userActivity.webpageURL];
[self.appEventsUtility saveCampaignIDs:userActivity.webpageURL];
[self logAutoSetupStatus:YES source:@"scenedelegate_ul"];
} named:[NSString stringWithFormat:@"AEMSceneUniversallinkAutoSetup_%@", NSStringFromClass(sceneClass)]];

[self.swizzler swizzleSelector:@selector(scene:willConnectToSession:options:) onClass:sceneClass withBlock:^(id sceneDelegate, SEL cmd, id scene, UISceneSession *session, UISceneConnectionOptions *options) {
[self.aemReporter enable];
for(UIOpenURLContext* urlContext in options.URLContexts) {
[self.aemReporter handle:urlContext.URL];
[self.appEventsUtility saveCampaignIDs:urlContext.URL];
}
[self logAutoSetupStatus:YES source:@"scenedelegate_coldstart"];
} named:[NSString stringWithFormat:@"AEMSceneColdStartAutoSetup_%@", NSStringFromClass(sceneClass)]];
}
}

- (NSSet<NSString *> *)getSceneDelegates
{
NSMutableSet<NSString *> *delegates = [NSMutableSet new];
NSDictionary<NSString *, id> *sceneManifest = [FBSDKTypeUtility dictionaryValue:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIApplicationSceneManifest"]];
NSDictionary<NSString *, id> *sceneConfigs = [FBSDKTypeUtility dictionaryValue:sceneManifest[@"UISceneConfigurations"]];
NSArray<id> *sceneSessionRoleApplicaiton = [FBSDKTypeUtility arrayValue:sceneConfigs[@"UIWindowSceneSessionRoleApplication"]];
for (id sceneSessionRole in sceneSessionRoleApplicaiton) {
NSDictionary<NSString *, id> *item = [FBSDKTypeUtility dictionaryValue:sceneSessionRole];
NSString *className = [FBSDKTypeUtility stringValueOrNil:item[@"UISceneDelegateClassName"]];
if (className) {
[delegates addObject:className];
}
}
return [delegates copy];
}

- (void)logAutoSetupStatus:(BOOL)optin
Expand Down

0 comments on commit 4509d5a

Please sign in to comment.