【问题标题】:openURL not being called when I launch the app from Scheme Url (Swift project)当我从 Scheme Url(Swift 项目)启动应用程序时未调用 openURL
【发布时间】:2019-11-07 11:59:03
【问题描述】:

当我通过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.
    }
}

知道会发生什么吗?

谢谢!

【问题讨论】:

  • 你必须在 didFinishLaunchingWithOptions() 中检查 launchOptions 字典。
  • 你在info.plist中添加了urlScheme吗?
  • @Jitendra launchOption 字典在 didFinishLaunchingWithOptions() 中为零。
  • @PGDev 我在 info.plist 中添加了 urlScheme

标签: ios swift xcode


【解决方案1】:

AppDelegateopenUrl方法的签名更新为,

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

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


对于 Swift-5 和 iOS-13:

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

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

【讨论】:

  • 我已经添加了我的 urlSchema。 URL很好的打开应用但是没有调用函数获取参数
  • 你试过把断点放在那里吗?因为它在我的最后工作正常。
  • 如果还是不行,尝试在一个全新的项目中实现同样的方法,看看是否可行。
  • 我创建了一个新项目,我所做的只是添加了 openURL 函数并添加了 url 类型,但它不起作用。
  • 从 safari 中打开 myapp:// 看看它是否正常工作。
【解决方案2】:

application(_ application: UIApplication, open url 不会在应用启动时调用。您需要在[UIApplication.LaunchOptionsKey.url] 的帮助下访问launchOptions: [UIApplication.LaunchOptionsKey: Any] 和您的网址

这里是可能对你有帮助的 sn-p

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

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

        return true
    }

【讨论】:

  • launchOptions 始终为 nil
  • 这有什么更新吗?我看到了同样的问题;打开 url 处理程序在应用程序启动时不会被调用,并且 launchOptions 为 nil。
【解决方案3】:

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

struct ShapifyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onOpenURL { url in
                    //Whatever you need to do goes here
                }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-12-26
    • 2016-01-07
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多