36

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.

Hejazi
  • 16,587
  • 9
  • 52
  • 67
Jabson
  • 1,585
  • 1
  • 12
  • 16

7 Answers7

77

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    I was wondering why below method was not called. application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { – Jitendra Nov 16 '19 at 13:29
  • Can you answer this? https://stackoverflow.com/questions/58973143/opening-app-using-custom-url-scheme-first-time-scene-openurlcontexts-does-n – iPhone Nov 21 '19 at 11:08
  • This was the tip for me. Thanks! – Shweta Jan 17 '20 at 11:08
  • Could you elaborate how to achieve it in `scene(_:willConnectTo:options:)`? Thanks. – Tianyao 'Till' Chen Jan 17 '20 at 16:43
  • 1
    @matt I was just hoping maybe you could provide a code snippet here:) – Tianyao 'Till' Chen Jan 18 '20 at 15:08
  • In my case none of these methods are being called, neither scene(_:openURLContexts:) nor scene(_:willConnectTo:options:). – Asad Ali Choudhry Apr 06 '20 at 08:28
  • @AsadAliChoudhry With a project created before Xcode 11, they would not be. – matt Apr 06 '20 at 12:25
  • my xcode is latest, 11.3.1. The other method scene( userActivity) is calling but only when app is in background, but when its not in background none of the methods is calling. – Asad Ali Choudhry Apr 06 '20 at 14:41
  • Will every active scene be called or does the OS just pick one? – powerj1984 Jul 21 '20 at 17:16
  • What if I'm currently working in Xcode 10. Does that mean openURL willnot be called at all in iOS 13+ ?? – chakkala Oct 20 '20 at 08:38
  • Thanks for the solution first of all. It took time to understand that `scene(_:openURLContexts:)` called when the app is loaded and presented in apps stack. But if the app IS CLOSED, conforms to some type and called from another app, `scene(_:willConnectTo:options:)` is being called instead of `openURLContexts` method. – ETech Jun 11 '21 at 16:54
28

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)
    }
}
Jitendra
  • 842
  • 1
  • 9
  • 25
23

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])        
}
SagarScript
  • 1,145
  • 11
  • 15
15

iOS 14 and above
For SwiftUI apps
Note: This method handles the reception of Universal Links
public func onOpenURL(perform action: @escaping (URL) -> ()) -> some View

@main
MyApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            RootView()
                .onOpenURL(perform: handleURL)
        }
    }

    func handleURL(_ url: URL) {

    }
}
Mike.R
  • 2,824
  • 2
  • 26
  • 34
webcpu
  • 3,174
  • 2
  • 24
  • 17
  • 1
    I've been searching for this for hours. Thank you so much!!!! – batuhankrbb Aug 17 '22 at 10:19
  • It is not worked when app is closed, only if app is in background. Add DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {...} inside .onOpenUrl to fix this – dibs Nov 03 '22 at 16:49
5

Thanks to @Matt there This is how I solve the problem.

on SceneDelegate.m

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {

    NSURL *url = connectionOptions.URLContexts.allObjects.firstObject.URL;
    NSLog(@"When app is not on memory  ::::: %@",url);

}

​
- (void)scene:(UIScene *)scene openURLContexts:(NSSet *)URLContexts {

    NSURL *url = URLContexts.allObjects.firstObject.URL;    
    NSLog(@"When app is on memory ::::: %@",url);

}
Peterworks
  • 435
  • 4
  • 13
  • I am facing same issue. I have posted here. Could you answer it? https://stackoverflow.com/questions/58973143/opening-app-using-custom-url-scheme-first-time-scene-openurlcontexts-does-n – iPhone Nov 21 '19 at 11:14
2

I had the same problem, and neither scene(_ scene:, continue userActivity:) nor scene(_ scene:, openURLContexts URLContexts:) was called.

When I checked the connectionOptions passed to the method scene(_ scene:, willConnectTo session, options connectionOptions:) it had a user activity with the expected URL. So, I ended up calling the method manually:

func scene(_ scene: UIScene,
           willConnectTo session: UISceneSession,
           options connectionOptions: UIScene.ConnectionOptions) {

    if let userActivity = connectionOptions.userActivities.first {
        self.scene(scene, continue: userActivity)
    }
}
Hejazi
  • 16,587
  • 9
  • 52
  • 67
0

I encountered a problem, using safari shortcut to start the application, openURLContexts callback multiple times.

2019-12-09 18:33:41.257088+0800 OCTEST[5019:1468254] openURLContextsopenURLContexts {(
    <UIOpenURLContext: 0x2805a71c0; URL: appoctest://action=123123123; options: <UISceneOpenURLOptions: 0x280bd4750; sourceApp: (null); annotation: (null); openInPlace: NO>>
)}
2019-12-09 18:33:42.022132+0800 OCTEST[5019:1468254] openURLContextsopenURLContexts {(
    <UIOpenURLContext: 0x2805a6d40; URL: appoctest://action=123123123; options: <UISceneOpenURLOptions: 0x280bd6f70; sourceApp: (null); annotation: (null); openInPlace: NO>>
)}
Sheldon
  • 1
  • 1