【发布时间】:2019-07-28 16:50:11
【问题描述】:
我想在我的 react-native 应用程序中处理自定义 URL 方案。因此,我首先在我的根组件内的 JS 代码中添加了一个 eventListener:
componentDidMount() {
Linking.addEventListener('url', this.handleOpenURL);
}
componentWillUnmount() {
Linking.removeEventListener('url', this.handleOpenURL);
}
handleOpenURL = event => {
console.log('event', event);
};
在我的 Xcode 应用程序中,我添加了一个自定义 URL 方案“com.myScheme”。
当我尝试在我的 AppDelegate.swift 中添加函数来处理深度链接时,我的问题出现了。
这是我的功能:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
print(url)
return true
}
似乎返回 true 不足以在我的应用程序中触发我的侦听器......
我知道 react-native 文档中有一个 Objective-C 方法:
#import <React/RCTLinkingManager.h>
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [RCTLinkingManager application:application openURL:url options:options];
}
但我想在我的项目中只使用 Swift,以提高我在这门语言方面的技能。问题是我找不到任何使用 Swift 实现的函数的示例。
这就是为什么我需要一些帮助来实现这个方法,然后在我的 RN 应用程序中触发我的事件。
感谢您的理解:)
【问题讨论】:
-
如何在swift中导入RCTLinkingManager?
标签: javascript ios swift xcode react-native