[英]How to open iPhone's default browser?
I placed a Facebook button in my iPhone app. 我在我的iPhone应用程序中放置了一个Facebook按钮。 How to open the iPhone's default browser when clicking on that button and loads the Facebook login page? 单击该按钮并加载Facebook登录页面时如何打开iPhone的默认浏览器?
iOS 10 has introduced new method to open a URL, see below: iOS 10引入了新方法来打开URL,见下文:
Swift 3: 斯威夫特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: 目标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. 当您打开iPhone的默认浏览器时,您的应用程序将进入后台 - 可能由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 https://github.com/facebook/facebook-ios-sdk
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/ 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 在目标C中
[[UIApplication sharedApplication] openURL:[NSURL
URLWithString:@"http://www.facebook.com"]];
Swift: iOS
var url:NSURL? = NSURL(string:webLink!)
UIApplication.sharedApplication().openURL(url!)
Swift 3: 斯威夫特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: 如果要添加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. OpenUrl已被删除,请使用以下代码。
NSURL *url = [NSURL URLWithString:@"https://google.com"];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.