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

标签 ios react-native

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

ComponentDidMount 中获取信息

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

我使用的是 react-native 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

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

最佳答案

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

关于ios - 为什么 Linking.getInitialURL 总是返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55482111/

相关文章:

ios - 室内导航如何在 iOS 中使用 iBeacon 或 wi-fi 工作?

iOS NSUserDefaults 检查 float 键是否存在

ios - 同一项目中的多个目标 - 但不是精简版与完整版

ios - React Native iOS 卡在 LaunchScreen 上

react-native - 阻止 YouTube 网站从 React-Native-WebView 重定向

ios - Objective-C 中的文字数组表示法

ios - 在ios中的一行uitableview中点击imageview(动态添加)

android - 连接到设备时 run-android 出现错误

javascript - 无法在 React Native 中显示图像

react-native - 将 Amplitude Analytics 集成到 React Native App 与 Expo