You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native iOS迁移AppDelegate至Swift后DeepLink失效问题

React Native iOS端DeepLink迁移Swift后失效问题

将React Native应用的AppDelegate从Objective-C迁移至Swift后,iOS端DeepLink功能异常:点击链接仅能打开应用,但无法跳转到指定路由,Android端功能正常。

原Objective-C版本AppDelegate代码

#import <React/RCTLinkingManager.h> 
// other imports

@implementation AppDelegate

// other stuff

- (BOOL)application:(UIApplication *)application
   openURL:(NSURL *)url
   options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  if ([[FBSDKApplicationDelegate sharedInstance] application:application openURL:url options:options]) {
    return YES;
  }

  if ([RCTLinkingManager application:application openURL:url options:options]) {
    return YES;
  }

  return NO;
}

- (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

迁移后的Swift版本AppDelegate代码

// imports

@main

// other stuff

func application(_ application: UIApplication, open URL: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if (ApplicationDelegate.shared.application(application, open: URL, options: options)) {
      return true
    }

    if (RCTLinkingManager.application(application, open: URL, options: options)) {
      return true;
    }

    return false
  }

  func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    return RCTLinkingManager.application(application, continue: userActivity, restorationHandler: restorationHandler)
  }

桥接文件配置(myApp-Bridging-Header.h)

// other imports
#import <React/RCTLinkingManager.h>

目前不确定是代码迁移有误,还是Swift版AppDelegate本身不支持DeepLink。

更新:Scene相关问题定位

在Scene中添加以下代码后,仅当应用处于前台或后台时DeepLink有效;冷启动应用时,仅会打开首页,无法跳转路由。

func scene( _ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        return
    }

    if RCTLinkingManager.application(UIApplication.shared, open: url, sourceApplication: nil, annotation: [UIApplication.OpenURLOptionsKey.annotation]) {
        return
    }
  }

  func scene( _ scene: UIScene, continue userActivity: NSUserActivity) {
      RCTLinkingManager.application(UIApplication.shared, continue: userActivity, restorationHandler: { _ in })
  }

内容的提问来源于stack exchange,提问作者Dani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 05:25:15