Apple URL 方案

这些是 iOS,OS X 和 watchOS 2 及更高版本上的本机应用程序支持的 URL 方案。

在 Safari 中打开链接:

Objective-C 的

NSString *stringURL = @"http://stackoverflow.com/";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

迅速:

let stringURL = "http://stackoverflow.com/"
if let url = URL(string: stringURL) {
    UIApplication.shared.openURL(url)
}

开始电话交谈

Objective-C 的

NSString *stringURL = @"tel:1-408-555-5555";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

迅速:

let stringURL = "tel:1-408-555-5555"
if let url = URL(string: stringURL) {
    UIApplication.shared.openURL(url)
}

HTML

<a href="tel:1-408-555-5555">1-408-555-5555</a>

开始 FaceTime 对话

Objective-C 的

NSString *stringURL = @"facetime:14085551234";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

迅速:

let stringURL = "facetime:14085551234"
if let url = URL(string: stringURL) {
    UIApplication.shared.openURL(url)
}

HTML

<a href="facetime:14085551234">Connect using FaceTime</a>
<a href="facetime:user@example.com">Connect using FaceTime</a>

打开消息应用程序以将短信撰写为收件人:

Objective-C 的

NSString *stringURL = @"sms:1-408-555-1212";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

迅速:

let stringURL = "sms:1-408-555-1212"
if let url = URL(string: stringURL) {
    UIApplication.shared.openURL(url)
}

HTML

<a href="sms:">Launch Messages App</a>
<a href="sms:1-408-555-1212">New SMS Message</a>

打开邮件应用以撰写发送给收件人的电子邮件:

Objective-C 的

NSString *stringURL = @"mailto:foo@example.com";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

迅速:

let stringURL = "mailto:foo@example.com"
if let url = URL(string: stringURL) {
    UIApplication.shared.openURL(url)
}

HTML

<a href="mailto:frank@wwdcdemo.example.com">John Frank</a>

你还可以在收件人抄送密件抄送字段中包含主题字段,邮件和多个收件人。 (在 iOS 中,将忽略 from 属性。)以下示例显示包含多个不同属性的 mailto URL:

mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!

注意: Compose 电子邮件对话框也可以使用 MFMailComposeViewController 在 app 中显示。