首页 > 解决方案 > 当我从 Scheme Url(Swift 项目)启动应用程序时未调用 openURL

问题描述

当我通过myapp://param1=abcopen 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.
    }
}

知道会发生什么吗?

谢谢!

标签: iosswiftxcode

解决方案


openUrl方法 in的签名AppDelegate更新为,

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

您需要在项目设置的信息选项卡下urlSchemeURL 类型中添加您的,即

在此处输入图像描述


对于 Swift-5 和 iOS-13:

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

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

推荐阅读