ios 如何打开 iPhone 的默认浏览器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8755843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:10:21  来源:igfitidea点击:

How to open iPhone's default browser?

iphoneiosfacebookuibutton

提问by Arun

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?

我在我的 iPhone 应用程序中放置了一个 Facebook 按钮。单击该按钮并加载 Facebook 登录页面时,如何打开 iPhone 的默认浏览器?

回答by Adil Soomro

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];
}

回答by Ankit Srivastava

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

回答by JOM

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.

当您打开 iPhone 的默认浏览器时,您的应用程序进入后台 - 可能被 iOS 终止。这真的是你想要的吗?用户无法返回到您的应用程序。

Couple options to consider:

需要考虑的几个选项:

  • Integrate FacebookiOS 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
  • 将 FacebookiOS 库集成到您的应用程序中。大量编码,因为您需要编写一些 UI 代码来支持集成。另一方面,它是您的应用程序,您可以控制正在发生的事情以及如何对用户操作做出反应

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

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

  • Embed browserinside 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/

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.

好吧,您知道所有详细信息并基于您想要的默认浏览器。没关系。

回答by Nitesh

In Swift

在斯威夫特

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

In objective C

在目标 C

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

回答by Vinod Joshi

Swift: iOS

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

回答by lenooh

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
    })
}

回答by Saeid N.

OpenUrl is depricated, Use the following code.

OpenUrl 已弃用,使用以下代码。

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