发布于 2022-11-18 13:11:49
该事件需要在AppDelegate.m文件中进行一些额外的配置,以发出文档中提到的事件。要么从XCode打开项目并编辑AppDelegate.m,要么打开./ios/{YOUR_PROJECT_NAME}/AppDelegate.m (或AppDelegate.mm)文件,并在@end标记出现之前在文件末尾添加以下行:
// Add this inside `@implementation AppDelegate` above `@end`:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [RCTLinkingManager application:application openURL:url options:options];
}
// Add this inside `@implementation AppDelegate` above `@end`:
- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
return [RCTLinkingManager application:application
continueUserActivity:userActivity
restorationHandler:restorationHandler];
}
@end 重要:如果你能做到这一点,你有两种方法来处理深度链接事件,并且你必须分开处理它们!我想第二个会帮你解决问题的。
1-应用程序已关闭,并将由深度链接打开:
Linking.getInitialURL().then(url => {
if(url != null) {
//DoSomethingWithUrl
}
});2-应用程序已经在运行,并将使用deeplink进行聚焦:
Linking.addEventListener('url',(url)=>{
if(url != null) {
//DoSomethingWithUrl
}
});将这些行放在您的应用程序视图中,并假设您的应用程序有某种状态(例如使用useState钩子或redux),它将调用发生的每个状态更改,因为状态本身旁边的所有内容都将在更改状态时重新呈现。因此,我建议您在app启动时只调用这两种方法一次,您可以这样做:
const [isInitialStart, setInitialStart] = useState(true);
if(isInitialStart){
Linking.getInitialURL().then(url => {
if(url != null) {
//DoSomethingWithUrl
}
});
Linking.addEventListener('url',(url)=>{
if(url != null) {
//DoSomethingWithUrl
}
});
setInitialStart(false);
}我希望这能帮助你解决你的问题。
https://stackoverflow.com/questions/73734083
复制相似问题