Linking.addEventListener 在 react native 中不起作用



我已经成功实现了一个通用链接,可以在我的应用程序中打开特定页面(如果应用程序已关闭(。 问题是,如果应用程序在后台运行,则不会调用事件侦听器。 这是代码:

import {Linking} from 'react-native';
export default class App extends React.Component {
async componentDidMount(){
Linking.addEventListener('url', this._handleOpenURL);
let url = await Linking.getInitialURL();
if (url) {
console.log('MOUNT GET INIT URL','initial url  ' + url);
}
}
_handleOpenURL = (event) => {
console.log("in _handleOpenURL", event.url)
}
}

MOUNT GET INIT URL已成功登录到控制台。 从不记录in _handleOpenURL。 似乎互联网上的其他人也有这个问题,但没有人回答。 有谁知道该怎么做?

在appdelegate.m 中最后一个@end之前添加以下内容:

// iOS 9.x or newer

- (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];
}

最新更新