7

I'm using the method openURL:options:completionHandler:, it turns out that in iOS 10 works fine, but I'm also interested in my app is compatible with the old iOS 9, but xcode gives me a

NSException:
-[UIApplication openURL:options:completionHandler:]:

Unrecognized selector send to instance There any way make it work in iOS 9 also? Thank for the possible response!

Maulik Pandya
  • 2,200
  • 17
  • 26
paco valero
  • 73
  • 1
  • 1
  • 3

3 Answers3

16

The new UIApplication method openURL:options:completionHandler:, which is executed asynchronously and calls the specified completion handler on the main queue (this method replaces openURL:)

This is under Additional Framework Changes > UIKit at: https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html

you need use it like this:-

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
    UIApplication.shared.openURL(url)
}
Annie Gupta
  • 2,786
  • 15
  • 23
  • I'm using openUrl(url, options, completionHandler) Shouldn't you switch open method with the openURL method? – Vincent Feb 20 '17 at 15:58
6

New method in iOS 10:

- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options
  completionHandler:(void (^ __nullable)(BOOL success))completion

Read Doc here:

https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html

The new UIApplication method openURL:options:completionHandler:, which is executed asynchronously and calls the specified completion handler on the main queue (this method replaces openURL:).

For below iOS 10:

[[UIApplication sharedApplication] openURL:URL];//URL is NSURL

You can use below code:

UIApplication *application = [UIApplication sharedApplication];

NSURL *URL = [NSURL URLWithString:strUrl];

if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){

  if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
    [application openURL:URL options:@{}
       completionHandler:^(BOOL success) {
      NSLog(@"Open %@: %d",scheme,success);
    }];
  } else {
    BOOL success = [application openURL:URL];
    NSLog(@"Open %@: %d",scheme,success);
  }


}
else{

  bool can = [application canOpenURL:URL];

  if(can){

     [application openURL:URL];

  }

}

Also need to set LSApplicationQueriesSchemes in plist if not set:

Like,

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>urlscheme1</string>
 <string>urlscheme2</string>

</array> 

Also read answer here: https://stackoverflow.com/a/40042291/5575752

Community
  • 1
  • 1
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
0

You can also use it like as below for obj-c.

[[UIApplication sharedApplication] openURL: url options:@{} completionHandler:nil];
Hardik Shekhat
  • 1,680
  • 12
  • 21