嗨,我试图使用React-Native的链接库来收听链接更改,我按照https://facebook.github.io/react-native/docs/linking.html上的说明进行操作.我可以使用openURL打开外部URL,但Linking.addEventListener似乎不适合我.我复制了代码段:
componentDidMount() {
Linking.addEventListener('url', this._handleOpenURL);
},
componentWillUnmount() {
Linking.removeEventListener('url', this._handleOpenURL);
},
_handleOpenURL(event) {
console.log(event.url);
}Run Code Online (Sandbox Code Playgroud)
它不会给我一个错误,但是当应用程序打开外部URL时,不会调用_handleOpenURL.
我想知道为什么会这样,我该怎么做才能解决它?
Car*_*oga 11
这是因为当应用程序通过一个意图启动时,链接有一个特定的方法。
试试这个:
componentDidMount() {
Linking.getInitialURL().then((ev) => {
if (ev) {
this._handleOpenURL(ev);
}
}).catch(err => {
console.warn('An error occurred', err);
});
Linking.addEventListener('url', this._handleOpenURL);
}
Run Code Online (Sandbox Code Playgroud)
小智 9
我遇到了同样的问题,我设法解决了 2 天。这是我的步骤,希望对下一个需要设法解决此问题的人有所帮助。
就我而言,我只是添加了这个方法
// iOS 9.x or newer
#import <React/RCTLinkingManager.h>
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [RCTLinkingManager application:application openURL:url options:options];
}
Run Code Online (Sandbox Code Playgroud)
之后,事件侦听器将正常工作。检查 IOS 版本,有 8.x 及以下的代码段。
| 归档时间: |
|
| 查看次数: |
6123 次 |
| 最近记录: |