简体   繁体   中英

How to open iPhone's default browser?

I placed a Facebook button in my iPhone app. How to open the iPhone's default browser when clicking on that button and loads the Facebook login page?

iOS 10 has introduced new method to open a URL, see below:

Swift 3:

let url = URL(string:"http://www.booleanbites.com")!
if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
    // Fallback on earlier versions
    UIApplication.shared.openURL(url)
}

Objective C:

NSURL *url = [NSURL URLWithString:@"http://www.booleanbites.com"];

if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL];
}else{
    // Fallback on earlier versions
    [[UIApplication sharedApplication] openURL:url];
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.facebook.com"]];

When you open iPhone's default browser, your application gets to background - possibly terminated by iOS. Is that really what you want? User has no way to get back to your app.

Couple options to consider:

  • Integrate Facebook iOS library into your application. Lots of coding, since you need to write some UI code to support the integration. On the other hand it's your application and you have some control over what is going on and how to react to user actions

https://github.com/facebook/facebook-ios-sdk

  • Embed browser inside your application and "open the browser" within your app. Yet again you have some control over what's going on and what to do about it

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/

The point is: it's YOUR application. Why would you force user to shutdown your app and start using something else without any way to get back?

Well, you know all the details and based on those you want the default browser. That's ok.

In Swift

UIApplication.sharedApplication().openURL(NSURL(string:"http://www.facebook.com")!)

In objective C

[[UIApplication sharedApplication] openURL:[NSURL          
URLWithString:@"http://www.facebook.com"]];
Swift: iOS

var url:NSURL? = NSURL(string:webLink!)
UIApplication.sharedApplication().openURL(url!)

Swift 3:

let url = URL(string: "http://www.example.com")!           
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

If you want to add a completionHandler:

let url = URL(string: "http://www.example.com")!           
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
        print("success!") // do what you want
    })
}

OpenUrl is depricated, Use the following code.

NSURL *url = [NSURL URLWithString:@"https://google.com"];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM