Custom URL opening app, but not passing information

Has anyone else run into an error where a custom URI such as "myapp://test" will open the app with a urlscheme "myapp" but won't pick up the "test" portion of the URI? The code I am using below is in the AppDelegate.swift file:


func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        @Binding var _: String = url.absoluteString
        print(url.absoluteString)
        Controller.message = url.absoluteString
        return true
    }


The Controller is a struct with a static var "message" that I am using in a View. Nothing is printed out when I pass "myapp://test", but the app opens even if it is close.


This does work, however, if I place this code in the SceneDelegate:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        if let url = URLContexts.first?.url{
            @Binding var _: String = url.absoluteString
            print(url.absoluteString)
            Controller.message = url.absoluteString
            print(Controller.message)
        }
    }


This will print the correct URI, but if I try to use this URI when the app is closed out it will open, but no information will be passed.


Is this a Swift 5.1 problem? Every tutorial/video I have found online uses the first code snippet in the AppDelegate file and I have triple checked to make sure it is the same. Everything is the same from the tutorials, but I'm still not getting any results.


Thanks.

Matt

Post not yet marked as solved Up vote post of micma418 Down vote post of micma418
4.3k views

Replies

For future stumblers:

When the app is killed and opened by a custom URI, it doesn't trigger func scene(_:openURLContexts:) but by the normal startup function func scene(_:willConnectTo:options) where connectionOptions contains your urlContexts.

Code Block Swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let myRootController = Controller()
window.rootViewController = myRootController
self.window = window
window.makeKeyAndVisible()
self.handle(urlContexts)
}
func scene(_ scene: UIScene, openURLContexts urlContexts: Set<UIOpenURLContext>) {
self.handle(connectionOptions.urlContexts)
}
private func handle(_ urlContexts: Set<UIOpenURLContext>) {
if let url = urlContexts.first?.url{
@Binding var _: String = url.absoluteString
print(url.absoluteString)
Controller.message = url.absoluteString
print(Controller.message)
}
}


  • I still can't get the information from the custom url when the app is opened from killed state using the SceneDelegate and the code sample above? What could be the issue?

  • Amazing. It worked for me.

  • great

Add a Comment

If I'm not mistaken, the two calls to handle() are reversed-- willConnectTo should have the one with connectionOptions.urlContexts and openURLContexts should have just urlContexts. Otherwise, this solved it for me.