我已成功使用深层链接从共享扩展程序打开我的应用程序。如果应用程序已经在运行,那么我使用 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/