在设置页面上,我想包括三个链接到
我不想在我的应用程序中打开网页。我只想将链接发送到Safari,该链接在那里打开。我已经看到许多应用程序在其设置页面上执行相同的操作,因此必须有可能。
我所做的就是我所做的:
- (IBAction)openDaleDietrichDotCom:(id)sender;
I在设置页面上添加了一个Uibutton,其中包含我要链接到的文本。
i适当地将按钮连接到文件所有者中的ibaction。然后实现以下内容:
- (IBAction)openDaleDietrichDotCom:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.daledietrich.com"]];
}
(ibaction在ViewController中,而不是标题文件)
if let link = URL(string: "https://yoursite.com") {
UIApplication.shared.open(link)
}
注意,我们不需要逃脱字符串和/或地址,例如:
let myNormalString = "https://example.com";
let myEscapedString = myNormalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
实际上,逃脱可能导致开放失败。
swift语法:
UIApplication.sharedApplication().openURL(NSURL(string:"http://www.reddit.com/")!)
iOS 9.3和更早的新闻迅速语法
在某些新版本的Swift(可能是Swift 2?)中,uiapplication.sharedapplication()现在是Uiapplication.shared(可以更好地利用我猜测的计算属性)。另外,URL不再隐式转换为nsurl,必须明确转换为!
UIApplication.sharedApplication.openURL(NSURL(string:"http://www.reddit.com/") as! URL)
iOS 10.0开始新的Swift语法
openURL方法已被弃用并用更通用的方法替换,该方法采用了选项对象和一个异步完成处理程序,从iOS 10.0开始
UIApplication.shared.open(NSURL(string:"http://www.reddit.com/")! as URL)
在此需要检查一个检查,即可通过设备或模拟器打开URL是否打开。因为有时(模拟器中的多数)我发现它会导致崩溃。 对象c
NSURL *url = [NSURL URLWithString:@"some url"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
let url : NSURL = NSURL(string: "some url")!
if UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(url)
}
guard let url = URL(string: "some url") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
查看uiapplication的
-openURL:
方法。它应该允许您将NSURL实例传递给系统,该实例将确定要在其中打开的应用程序并启动该应用程序。 (请记住,您可能需要首先检查
-canOpenURL:
,以防万一无法通过系统上安装的应用程序处理该URL - 尽管这可能不是Pline
http://
链接的问题。))
,如果您不确定提供的URL是否有一个方案:text
未弃用的Objective-C版本将是:
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];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://apple.com"] options:@{} completionHandler:nil];
因此,我们将不得不使用此初始化器:SFSafariViewController(url: url, entersReaderIfAvailable: true) // was deprecated in iOS 11.0
SFSafariViewController(url: URL, configuration: SFSafariViewController.Configuration)
用完成按钮的解决方案 不要忘记
if let url = URL(string: "http://www.yoururl.com/") {
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = true
let vc = SFSafariViewController(url: url, configuration: config)
present(vc, animated: true)
}
import SafariServices
if let url = URL(string: "http://www.yoururl.com/") {
let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
present(vc, animated: true)
}
这支持运行iOS旧版本的设备以及
由于iOS 10.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)
}
}
thouge en repocients-cif #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}else{
UIApplication.shared.openURL(url)
}
swift 5:
[[UIApplication sharedApplication] openURL:@"url string" options:@{} completionHandler:^(BOOL success) {
if (success) {
NSLog(@"Opened url");
}
}];
usage:
func open(scheme: String) {
if let url = URL(string: scheme) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(scheme): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(scheme): \(success)")
}
}
}
ios10
中的openurl
ios 10.0中对openurl(
:)
if let url = URL(string: "https://apple.com") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
trone this:
if let url = URL(string: "https://www.google.com") {
UIApplication.shared.open(url, options: [:])
}
在Swift 1.2,尝试以下操作:
NSString *URL = @"xyz.com";
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:URL]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL]];
}
Swift4解决方案:
let pth = "http://www.google.com"
if let url = NSURL(string: pth){
UIApplication.sharedApplication().openURL(url)