首页 > 解决方案 > Linking.addEventListener 在本机反应中不起作用

问题描述

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

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从不记录。似乎互联网上的其他人也遇到了这个问题,但没有人回答。有谁知道该怎么做?

标签: react-nativeios-universal-linksreact-native-linking

解决方案


@end在 appdelegate.m中的最后一个之前添加:

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

推荐阅读