我正在成功地使用深度链接从共享扩展打开我的应用程序。如果应用程序已经在运行,那么我将使用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。我希望获得启动应用程序的链接,即使它没有运行。
发布于 2019-05-23 02:02:37
如果我在远程调试,得到的响应总是null。如果远程调试器关闭了,那么我会得到正确的url。我使用了一个警报来确认,因为我不能console.log。
发布于 2019-05-01 10:17:33
还没能让getInitialURL()正常工作。但是,您可以使用:
Linking.addEventListener('url',(url)=>{
console.log('this is the url: ',url);
});这将为您提供url,请确保在componentWillUnmount()上取消注册该事件
此外,我还尝试恢复到以前对我有效的react-native版本,但是仍然失败,非常奇怪,这个问题发生了。
发布于 2021-04-30 05:19:34
对于正在寻找快速解决方案的每个人,只需手动将Linking.getInitialURL()的结果与您的应用程序链接的值交换即可,例如:myapp://myhost。
如果你不知道什么值,你应该将refet放在本教程https://blog.jscrambler.com/how-to-handle-deep-linking-in-a-react-native-app/中。
https://stackoverflow.com/questions/55482111
复制相似问题