无法读取传入的深度 link iOS Swift

Unable to read incoming deep link iOS Swift

最低iOS版本为13,Scene delegate文件被完全删除,只使用appdelegate。但是 open urlcontinue userActivityopenURLContexts 方法在使用 deep link 打开应用程序时没有调用。

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        
        print("app opened using deep link from \(sourceApplication)")
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DeepLink"), object: nil, userInfo: nil)
    
        return true
    }
    



    func application(_ application: UIApplication, continue userActivity:
            NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    
        print("app opened using deep link ")
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DeepLink"), object: nil, userInfo: nil)
    
        return true
        }



    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        if let url = URLContexts.first?.url{
            print(url)
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DeepLink"), object: nil, userInfo: nil)

        }
    }

编辑 - 1

Invalid redeclaration of 'application(_:open:options:)'

这是我创建的 Sample App 用于测试深度链接的最小值 iOS 13 没有 SceneDelegate

这是因为您可能使用了错误的打开 URL 方法来接收深层链接:
正确的是:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    print("Deep link received \(url)")
    return true
}

继续userActivity一般在通用链接的情况下调用

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

因此,AppDelegate 看起来像:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        print("Deep link received \(url)")
        return true
    }
}

并在 Info.plist 中确保添加 URL 方案。

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string></string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>deepLinks</string>
        </array>
    </dict>
</array>

看起来像这样:

然后要测试您的深层链接,请转到您的浏览器并使用 :// 编写您的 uri 方案

本例中的示例:
deepLinks://mydeeplinkurl

它会要求你打开你的应用程序。

你会在 open URL 方法中得到一个回调