Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method 'application:openURL:options:' is not called

I'm trying to open my app from a web page using custom schemes. The app is opened but the following method is not called:

func application(_ app: UIApplication, open url: URL, options [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    // This is not called
}

My info.plist looks like the following:

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>MyApp</string>
            </array>
            <key>CFBundleURLName</key>
            <string>url here</string>
        </dict>
    </array>

The project is created with Xcode 11.1, and I'm testing on iOS 13.

like image 290
Jabson Avatar asked Oct 30 '19 11:10

Jabson


3 Answers

Implement scene(_:openURLContexts:) in your scene delegate.

If the URL launches your app, you will get scene(_:willConnectTo:options:) instead and it’s in the options.

like image 127
matt Avatar answered Oct 10 '22 17:10

matt


Here is the method in SceneDelegate.swift. Swift version For iOS13

@available(iOS 13.0, *)
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url {
        // Handle URL
        WXApi.handleOpen(url, delegate: AppDelegate.shared)
    }
}
like image 34
Jitendra Avatar answered Oct 10 '22 17:10

Jitendra


With the latest SDK, this does work fine if you are at NOT using SceneDelegate.

If you are using sceneDelegate the the following AppDelegate method is not called and therefore the login cannot be handled.

func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let handled = ApplicationDelegate.shared.application(
        application,
        open: url,
        sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
        annotation: options[UIApplication.OpenURLOptionsKey.annotation])
    return handled
}

This is because, this method is (understandably) deferred to the following method in the SceneDelegate:

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

The solution which I can confirm as working for iOS 13 applications implementing a SceneDelegate is:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        return
    }
    let _ = ApplicationDelegate.shared.application(
        UIApplication.shared,
        open: url,
        sourceApplication: nil,
        annotation: [UIApplication.OpenURLOptionsKey.annotation])        
}
like image 28
Sagar Patel Avatar answered Oct 10 '22 17:10

Sagar Patel