如何从 iOS 应用程序启动 Safari 并打开 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12416469/
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
How to launch Safari and open URL from iOS app
提问by Dale Dietrich
On the settings page, I want to include three links to
在设置页面上,我想包含三个链接
- My app support site
- YouTube app tutorial
- My primary site (ie: linked to a 'Created by Dale Dietrich' label.)
- 我的应用程序支持站点
- YouTube 应用教程
- 我的主要站点(即:链接到“由 Dale Dietrich 创建”标签。)
I've searched this site and the web and my documentation and I've found nothing that is obvious.
我搜索了这个网站、网络和我的文档,但没有发现任何明显的东西。
NOTE:I don't want to open web pages within my app. I just want to send the link to Safari and that link be open there. I've seen a number of apps doing the same thing in their Settings page, so it must be possible.
注意:我不想在我的应用程序中打开网页。我只想将链接发送到 Safari,然后在那里打开该链接。我在他们的“设置”页面中看到许多应用程序在做同样的事情,所以这一定是可能的。
回答by Dale Dietrich
Here's what I did:
这是我所做的:
I created an IBAction in the header .h files as follows:
- (IBAction)openDaleDietrichDotCom:(id)sender;
I added a UIButton on the Settings page containing the text that I want to link to.
I connected the button to IBAction in File Owner appropriately.
Then implement the following:
我在头文件 .h 中创建了一个 IBAction,如下所示:
- (IBAction)openDaleDietrichDotCom:(id)sender;
我在“设置”页面上添加了一个 UIButton,其中包含我想要链接到的文本。
我将按钮适当地连接到文件所有者中的 IBAction。
然后执行以下操作:
Objective-C
目标-C
- (IBAction)openDaleDietrichDotCom:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.daledietrich.com"]];
}
Swift
迅速
(IBAction in viewController, rather than header file)
(viewController 中的 IBAction,而不是头文件)
if let link = URL(string: "https://yoursite.com") {
UIApplication.shared.open(link)
}
回答by Dustin Williams
Swift Syntax:
快速语法:
UIApplication.sharedApplication().openURL(NSURL(string:"http://www.reddit.com/")!)
New Swift Syntax for iOS 9.3 and earlier
适用于 iOS 9.3 及更早版本的新 Swift 语法
As of some new version of Swift (possibly swift 2?), UIApplication.sharedApplication() is now UIApplication.shared (making better use of computed properties I'm guessing). Additionally URL is no longer implicitly convertible to NSURL, must be explicitly converted with as!
从 Swift 的一些新版本(可能是 swift 2?)开始, UIApplication.sharedApplication() 现在是 UIApplication.shared (我猜是为了更好地利用计算属性)。另外 URL 不再隐式转换为 NSURL,必须显式转换为 as!
UIApplication.sharedApplication.openURL(NSURL(string:"http://www.reddit.com/") as! URL)
New Swift Syntax as of iOS 10.0
从 iOS 10.0 开始的新 Swift 语法
The openURL method has been deprecated and replaced with a more versatile method which takes an options object and an asynchronous completion handler as of iOS 10.0
openURL 方法已被弃用,取而代之的是一种更通用的方法,该方法从 iOS 10.0 开始采用选项对象和异步完成处理程序
UIApplication.shared.open(NSURL(string:"http://www.reddit.com/")! as URL)
回答by Chetan Prajapati
Here one check is required that the url going to be open is able to open by device or simulator or not. Because some times (majority in simulator) i found it causes crashes.
这里需要进行一项检查,确定要打开的 url 是否能够被设备或模拟器打开。因为有时(模拟器中的大多数)我发现它会导致崩溃。
Objective-C
目标-C
NSURL *url = [NSURL URLWithString:@"some url"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
Swift 2.0
斯威夫特 2.0
let url : NSURL = NSURL(string: "some url")!
if UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(url)
}
Swift 4.2
斯威夫特 4.2
guard let url = URL(string: "some url") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
回答by Tim
Take a look at the -openURL:
method on UIApplication. It should allow you to pass an NSURL instance to the system, which will determine what app to open it in and launch that application. (Keep in mind you'll probably want to check -canOpenURL:
first, just in case the URL can't be handled by apps currently installed on the system - though this is likely not a problem for plain http://
links.)
看一下-openURL:
UIApplication上的方法。它应该允许您将 NSURL 实例传递给系统,系统将决定在哪个应用程序中打开它并启动该应用程序。(请记住,您可能需要先检查一下-canOpenURL:
,以防万一系统上当前安装的应用程序无法处理该 URL - 尽管这对于普通http://
链接来说可能不是问题。)
回答by meaning-matters
And, in case you're not sure if the supplied URL text
has a scheme:
而且,如果您不确定所提供的 URLtext
是否具有方案:
NSString* text = @"www.apple.com";
NSURL* url = [[NSURL alloc] initWithString:text];
if (url.scheme.length == 0)
{
text = [@"http://" stringByAppendingString:text];
url = [[NSURL alloc] initWithString:text];
}
[[UIApplication sharedApplication] openURL:url];
回答by Antzi
The non deprecated Objective-C version would be:
未弃用的 Objective-C 版本将是:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://apple.com"] options:@{} completionHandler:nil];
回答by Greg T
Swift 3.0
斯威夫特 3.0
if let url = URL(string: "https://www.reddit.com") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:])
} else {
UIApplication.shared.openURL(url)
}
}
This supports devices running older versions of iOS as well
这也支持运行旧版本 iOS 的设备
回答by Baher A
Swift 3Solution with a Done button
带有“完成”按钮的Swift 3解决方案
Don't forget to import SafariServices
不要忘记 import SafariServices
if let url = URL(string: "http://www.yoururl.com/") {
let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
present(vc, animated: true)
}
回答by wazowski
Because this answer is deprecated since iOS 10.0, a better answer would be:
由于此答案自 iOS 10.0 起已弃用,因此更好的答案是:
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}else{
UIApplication.shared.openURL(url)
}
y en Objective-c
y en Objective-c
[[UIApplication sharedApplication] openURL:@"url string" options:@{} completionHandler:^(BOOL success) {
if (success) {
NSLog(@"Opened url");
}
}];
回答by Ryan H.
openURL(:)was deprecated in iOS 10.0, instead you should use the following instance method on UIApplication: open(:options:completionHandler:)
openURL( :)在 iOS 10.0 中已弃用,您应该在 UIApplication 上使用以下实例方法:open(:options:completionHandler :)
Example using Swift
This will open "https://apple.com" in Safari.
使用 Swift 的示例
这将在 Safari 中打开“ https://apple.com”。
if let url = URL(string: "https://apple.com") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
https://developer.apple.com/reference/uikit/uiapplication/1648685-open
https://developer.apple.com/reference/uikit/uiapplication/1648685-open