为什么 Linking.getInitialURL 总是返回 null?

chi*_*rry 8 ios react-native

我成功地使用深层链接从共享扩展打开我的应用程序。如果应用程序已经在运行,那么我会在ComponentDidMount使用中获取信息Linking.addEventListener('url', this.handleOpenURL);

如果应用程序尚未运行,我希望能够Linking.getInitialURL();根据文档(https://facebook.github.io/react-native/docs/linking)使用。除了它总是出现null在我身上。该链接是我的应用程序的打开方式,但无论如何,都显示为空。我在 App.js 的 componentDidMount 中有该代码。

我在反应原生 59.2。

这是我的 AppDelegate.m

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTLinkingManager.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"MYMODULENAME"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [UIColor blackColor];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [RCTLinkingManager application:application openURL:url options:options];
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
 restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
  return [RCTLinkingManager application:application
                   continueUserActivity:userActivity
                     restorationHandler:restorationHandler];
}

@end

Run Code Online (Sandbox Code Playgroud)

如果应用程序已经在后台运行,则getInitialURL()返回启动它的链接,否则我会得到 null。我希望获得启动应用程序的链接,即使它没有运行。

chi*_*rry 31

null 如果我在远程调试,响应总是如此。如果远程调试器关闭,那么我会得到正确的 url。我使用警报进行确认,因为我无法使用 console.log。

  • 我已经为此苦苦挣扎了几个小时!拯救了我的一天 (2认同)

div*_*hch 6

还没能getInitialURL()上班。但是您可以使用:

Linking.addEventListener('url',(url)=>{ 
    console.log('this is the url: ',url);
});
Run Code Online (Sandbox Code Playgroud)

这将为您提供 url,请确保取消注册该事件componentWillUnmount()

我还尝试恢复到以前对我有用的反应本机版本,但是仍然失败,所以这个问题的发生非常奇怪。

  • 任何正在阅读本文的人都知道:这不会让您从打开应用程序的意图中获得初始 URL(至少在 Android 中)。为此,您需要 getInitialURL (2认同)