简体   繁体   中英

Firebase Dynamic Links on iOS/React Native only working when app is running

I am using Firebase's Dynamic Links (8.11.0) to open a React Native (0.66.4) app with a specific URL.

In my AppDelegate.m file, I'm handling the URL this way:

- (BOOL)application:(UIApplication *)application
continueUserActivity:(nonnull NSUserActivity *)userActivity
 restorationHandler:
#if defined(__IPHONE_12_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0)
(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> *_Nullable))restorationHandler {
#else
  (nonnull void (^)(NSArray *_Nullable))restorationHandler {
#endif  // __IPHONE_12_0
        FIRDynamicLink *dynamicLink = [[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:userActivity.webpageURL];
    
        if (dynamicLink) {
          if (dynamicLink.url) {
            NSLog(@"dynamicLink.url %@", dynamicLink.url);
            return [RCTLinkingManager application:application openURL:dynamicLink.url options:[NSMutableDictionary dictionary]];
          }
        }
    
        return [[FIRDynamicLinks dynamicLinks] handleUniversalLink:userActivity.webpageURL
                                                 completion:^(FIRDynamicLink * _Nullable dynamicLink, NSError * _Nullable error) {
          if (error) {
            NSLog(@" dynamicLink.url ERROR %@", error);
          }
          if (dynamicLink) {
            if (dynamicLink.url) {
              NSLog(@" dynamicLink.url %@", dynamicLink.url);
              // ******** This line is executed as expected in both cases *******
              [RCTLinkingManager application:application openURL:dynamicLink.url options:[NSMutableDictionary dictionary]];
            } else {
              [RCTLinkingManager application:application openURL:userActivity.webpageURL options:[NSMutableDictionary dictionary]];
            }
          } else {
            [RCTLinkingManager application:application openURL:userActivity.webpageURL options:[NSMutableDictionary dictionary]];
          }
        }];
  }

My Info.plist file specifies my custom domain:

<key>FirebaseDynamicLinksCustomDomains</key>
<array>
  <string>https://join.mysite.app</string>
</array>

And my Associated Domains specify

applinks:join.mysite.app

With this in place, my React Native code, via Linking.getInitalURL() is receiving the link (specified here ) that I expect, but only if the app is already running when the short link is clicked .

When my app is not running, the same code noted in the comments above is executed, with the same inputs and outputs that I expect, but the Linking.getInitalURL() call is giving me the short link, not the resolved link that is passed to RCTLinkingManager .

I would expect that with the same code path/data, the behavior would be the same, but it's only correct when the app is already running. Am I missing a step?

From the sample, you shared you only implemented the continueUserActivity method that intercepts links when the application is already open.

You also need to implement openURL method like their documentation states here

In the end, I would highly advise using an already build library that works with react-native

If you really insist on building this functionality on your own, at least you can check out their source code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM