Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use openURL in iOS 10?

Tags:

ios10

I'm working on iOS 10 app and since openURL is deprecated I need some help using the new method. Problem I'm facing is not knowing what to pass in the options parameter.

Here's my code:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"] options:nil completionHandler:nil];

Compiler gives warning: "Null passed to a callee that requires a non-null argument."

Confused what I should pass in...?

like image 494
user6840013 Avatar asked Sep 16 '16 15:09

user6840013


3 Answers

You should write it like this:

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"] options:@{} completionHandler:nil];
like image 133
aNa12 Avatar answered Nov 01 '22 06:11

aNa12


For Swift 3 you should use this:

 UIApplication.shared().open(url: URL, options: [String: AnyObject], completionHandler: ((Bool) -> Void)?)

for example I used in my project:

let url = URL(string: "http://kaznews.kz")
UIApplication.shared.open(url!, options: [:], completionHandler: nil)

this is simplest way without options and handler.

like image 39
Yerbol Avatar answered Nov 01 '22 07:11

Yerbol


For iOS 10.2 and swift 3.1

private var urlString:String = "https://google.com"

@IBAction func openInSafari(sender: AnyObject) {
    let url = NSURL(string: self.urlString)!
    UIApplication.shared.open(url as URL, options: [ : ]) { (success) in
        if success{
            print("Its working fine")
        }else{
            print("You ran into problem")
        }            
    }
}
like image 1
Roshan Sah Avatar answered Nov 01 '22 05:11

Roshan Sah