简体   繁体   中英

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

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.

我的应用程序的 Info.plist

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"

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)
}
WindowGroup {
    SampleView()
        .onOpenURL { url in
            //this url that was opened by your app
        }
    }
}

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM