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:
https://github.com/facebook/facebook-ios-sdk
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.