pogbamessi
pogbamessi

Reputation: 333

Custom url scheme works only if the app is in the background?

I have defined a custom url scheme for my iOS app so that it can be accessed from a website. When I type my custom url scheme in safari the app opens up but the following function in my AppDelegate does not get called:

 func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    print("Scheme: \(url.scheme)");
    print("Query: \(url.absoluteString)");
    return true;
}

It gets called only when the app is open and running in the background. I want that function to be called when I type in the url in safari and the app is not open. Any help is appreciated, thanks!

Upvotes: 2

Views: 2751

Answers (3)

Alessandro Ornano
Alessandro Ornano

Reputation: 35402

Swift 4:

I've founded that sometimes seems the app cannot open the url simply due to the time needed to build it's phisic infrastructure (especially if you use customized viewcontrollers or transitions). I've found this solution:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
         DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
             // your code
         }
         return true
}

Upvotes: 1

Mike Gledhill
Mike Gledhill

Reputation: 29213

This almost sounds like the Custom URL settings haven't been setup correctly.

Do you definitely have these settings in your .plist file ?

enter image description here

With these settings in place, the application function should definitely get called, whenever your app gets launched.

More information here:

Custom URLs

Upvotes: 0

harmjanr
harmjanr

Reputation: 937

In your didFinishLaunchingWithOptions, you can check if the launchOptions contains the key UIApplicationLaunchOptionsURLKey. If you open your app using a scheme, it will be in that dictionary.

When the app is in the background, use openURL (as you do already).

Upvotes: 2

Related Questions