Search code examples
iosswifturl-schemeopenurl

What does the Swift 2 method signature for application:openURL:options: look like?


I'm working on the Swift version of an app that handles custom URL schemes.

The method you need to implement changed in iOS 9.

The Objective-C version of the method works fine in an Objective-C app:

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<NSString *,
                     id> *)options
{
  //my code here
}

However, in my Swift app, the equivalent function:

func application(application: UIApplication,
  openURL: NSURL,
  options: [String : AnyObject]) -> Bool
{
  //My code here
}

Is never called when I run the app on an iOS 9 device. When I invoke my custom URL scheme in Safari, I get prompted 'Open in "appname"?', and when I tap open, it brings my app back to the foreground, but the above method does not get called.

There must be some subtle mismatch in my method signature, but I can't see it. What am I doing wrong? I've tried various variations, none of which work.


Solution

  • My problem appears to have been a red herring caused by a corrupted project. I created a new project file and copied the same code in and in the new project, application:openURL:options: is called correctly.

    This is a very strange problem. If I delete "AppDelegate.swift" in the malfunctioning project and replace it with an AppDelegate.m/AppDelegate.h, then the application:openURL:options: is called correctly in the Objective-C version.

    My suspicion is that there is an intermittent bug in Xcode that causes some projects to fail to cal your app delegate's application:openURL:options: when the app delegate in Swift.

    If you are having the same problem you may want to create a new project, set up your info.plist, and copy over the application:openURL:options: method to see if the new project calls your method.