URL Opening Swift App - Open Works - Called Function Does NOT work

房东的猫 提交于 2019-12-30 09:57:30

问题


Hello All – I am a newbie here ... trying to get my first iOS Swift (Xcode 11.2.1 / Swift 5 / iOS 13.2) app to open via URL and process the URL string. The app opens just fine, but the function (method?) does not appear to be getting called. Here's what I've got:

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        print("Here I iz ... in the URL responding bit.")
        print(url)
        return true
    }

...

That code is in the AppDelegate.swift file in my project.

And that is what I have in my Info.plist file.

I launch via Safari both on-device and in the simulator with confirmevent://HelloWorld

As I said ... The app is opening, but I do not see any results from my print statements in the Xcode debug area.

In searching other posts they said that I need to use the "Wait for executable to be launched" to attach Xcode to the app, which I have done and it is indeed attaching. BUT I notice that none of my dozens of print statements that I have scattered about in my app appear when either when opening via "wait for executable to be launched" option.

Any/All help would be appreciated. I've spent over 5 hours scouring the web, but all indications are that is should "just work"


回答1:


I just found a solution here: application(...continue userActivity...) method not called in ios 13

The trick is to also implement the SceneDelegate.swift functions for Apps with iOS > 13. This function should be called if you open the URL confirmevent://HelloWorld:

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



回答2:


you need to use this function in your appDelegate

func application(_ application: UIApplication, continue userActivity: NSUserActivity,
                     restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        if let incomingURL = userActivity.webpageURL {
           print(incomingURL)
        }
        return false

}


来源:https://stackoverflow.com/questions/59076416/url-opening-swift-app-open-works-called-function-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!