从Scheme Url(Swift项目)启动应用程序时未调用openURL

当我通过myapp://param1=abc打开应用程序时,未调用open url函数。

正如Apple文档所说,我已经添加了功能didFinishlaunchingWithOptionswillFinishlaunchingWithOptions使其成为true,但仍未调用。该应用程序可以完美打开,但没有调用函数open url,我无法获取param1

这是我的AppDelegate文件:

class AppDelegate: UIResponder,UIApplicationDelegate  {

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

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

    func application(_ application: UIApplication,open url: URL,options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        // Here doesn't come! :(
        return true
    }


    // MARK: UIScenesession Lifecycle


    func application(_ application: UIApplication,configurationForConnecting connectingScenesession: UIScenesession,options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration",sessionRole: connectingScenesession.role)
    }

    func application(_ application: UIApplication,didDiscardScenesessions scenesessions: Set<UIScenesession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running,this will be called shortly after application:didFinishlaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes,as they will not return.
    }
}

有什么想法吗?

谢谢!

aa2463 回答:从Scheme Url(Swift项目)启动应用程序时未调用openURL

openUrlAppDelegate方法的签名已更新为

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

您需要在项目设置的信息标签下的 URL类型中添加urlScheme,即

enter image description here


对于Swift-5和iOS-13:

SceneDelegate.swift文件中,您需要实现以下方法来处理urlSchemes,即

func scene(_ scene: UIScene,openURLContexts URLContexts: Set<UIOpenURLContext>) {
    print(URLContexts)
}
,

application(_ application: UIApplication,open url在应用启动时将不会调用。您需要借助launchOptions: [UIApplication.LaunchOptionsKey: Any]

访问[UIApplication.LaunchOptionsKey.url]和您的网址

以下代码段可能会对您有所帮助

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

        let url = launchOptions![UIApplication.LaunchOptionsKey.url]
        ...

        return true
    }
,

从iOS 14开始,您可以改用深层链接:

idp.iss
本文链接:https://www.f2er.com/3145067.html

大家都在问