从我的应用打开 Gmail 应用
Open Gmail app from my app
我正在尝试从我的应用程序发送电子邮件。但我想要的是,如果用户在 his/her phone 上安装了 Gmail 应用程序,那么应该使用它发送邮件。如果 Gmail 应用程序不可用,则应将用户重定向到邮箱。
那么我如何知道用户是否包含 Gmail 应用程序以及如何将用户重定向到它。
您需要使用自定义 URL 方案。对于 gmail 应用程序:
googlegmail://
如果您想在那里撰写消息,可以向其中添加更多参数 URL:
co?subject=Example&body=ExampleBody
您可以使用此代码确定是否安装了任何类型的应用程序(对于其他应用程序,显然只需替换 customURL):
NSString *customURL = @"googlegmail://";
if ([[UIApplication sharedApplication]
canOpenURL:[NSURL URLWithString:customURL]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
}
else
{
//not installed, show popup for a user or an error
}
设置 iOS9+
如 here 所述,如果您使用 iOS9+,请不要忘记将 googlegmail
添加到 LSApplicationQueriesSchemes
在你的 info.plist
打开 GMail 的代码
然后,你可以按照接受的答案做同样的事情(下面是我的swift 2.3版本):
let googleUrlString = "googlegmail:///co?subject=Hello&body=Hi"
if let googleUrl = NSURL(string: googleUrlString) {
// show alert to choose app
if UIApplication.sharedApplication().canOpenURL(googleUrl) {
if #available(iOS 10.0, *) {
UIApplication.sharedApplication().openURL(googleUrl, options: [:], completionHandler: nil)
} else {
UIApplication.sharedApplication().openURL(googleUrl)
}
}
}
对于Swift 3.0+
备注:
- 此解决方案展示了如何在 URL 的参数中使用空格或换行符(Gmail 可能不遵守换行符)。
只要您不调用 canOpenURL(url),就无需注册 LSApplicationQueriesSchemes。只需尝试使用完成处理程序来确定它是否成功。
let googleUrlString = "googlegmail:///co?to=\(address.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? "")&subject=\(subject.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? "")&body=\(buildInfo.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? "")"
if let googleUrl = URL(string: googleUrlString) {
UIApplication.shared.open(googleUrl, options: [:]) {
success in
if !success {
// Notify user or handle failure as appropriate
}
}
}
else {
print("Could not get URL from string")
}
在我意识到我的目标是 info_development.plist 而不是生产文件 info.plist
之前,我无法弄清楚为什么这对我不起作用
如果您像我一样碰巧有多个 Plist(一个用于开发,一个用于生产等),请确保在任何地方都对其进行编辑。 ;-)
Swift 5
这些答案可以打开gmail 但如果用户没有在设备上安装gmail 怎么办?在那种情况下,我已经处理了打开苹果 mail/outlook/yahoo/spark。如果存在 none 个,我将显示警报。
@IBAction func openmailAction() {
if let googleUrl = NSURL(string: "googlegmail://") {
openMail(googleUrl)
} else if let mailURL = NSURL(string: "message://") {
openMail(mailURL)
} else if let outlookURL = NSURL(string: "ms-outlook://") {
openMail(outlookURL)
} else if let yahooURL = NSURL(string: "ymail://") {
openMail(yahooURL)
} else if let sparkUrl = NSURL(string: "readdle-spark://") {
openMail(sparkUrl)
} else {
// showAlert
}
}
func openMail(_ url: NSURL) {
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
}
您可能还需要将此添加到 plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlegmail</string>
<string>ms-outlook</string>
<string>readdle-spark</string>
<string>ymail</string>
</array>
我正在尝试从我的应用程序发送电子邮件。但我想要的是,如果用户在 his/her phone 上安装了 Gmail 应用程序,那么应该使用它发送邮件。如果 Gmail 应用程序不可用,则应将用户重定向到邮箱。
那么我如何知道用户是否包含 Gmail 应用程序以及如何将用户重定向到它。
您需要使用自定义 URL 方案。对于 gmail 应用程序:
googlegmail://
如果您想在那里撰写消息,可以向其中添加更多参数 URL:
co?subject=Example&body=ExampleBody
您可以使用此代码确定是否安装了任何类型的应用程序(对于其他应用程序,显然只需替换 customURL):
NSString *customURL = @"googlegmail://";
if ([[UIApplication sharedApplication]
canOpenURL:[NSURL URLWithString:customURL]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
}
else
{
//not installed, show popup for a user or an error
}
设置 iOS9+
如 here 所述,如果您使用 iOS9+,请不要忘记将 googlegmail
添加到 LSApplicationQueriesSchemes
在你的 info.plist
打开 GMail 的代码
然后,你可以按照接受的答案做同样的事情(下面是我的swift 2.3版本):
let googleUrlString = "googlegmail:///co?subject=Hello&body=Hi"
if let googleUrl = NSURL(string: googleUrlString) {
// show alert to choose app
if UIApplication.sharedApplication().canOpenURL(googleUrl) {
if #available(iOS 10.0, *) {
UIApplication.sharedApplication().openURL(googleUrl, options: [:], completionHandler: nil)
} else {
UIApplication.sharedApplication().openURL(googleUrl)
}
}
}
对于Swift 3.0+
备注:
- 此解决方案展示了如何在 URL 的参数中使用空格或换行符(Gmail 可能不遵守换行符)。
只要您不调用 canOpenURL(url),就无需注册 LSApplicationQueriesSchemes。只需尝试使用完成处理程序来确定它是否成功。
let googleUrlString = "googlegmail:///co?to=\(address.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? "")&subject=\(subject.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? "")&body=\(buildInfo.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? "")" if let googleUrl = URL(string: googleUrlString) { UIApplication.shared.open(googleUrl, options: [:]) { success in if !success { // Notify user or handle failure as appropriate } } } else { print("Could not get URL from string") }
在我意识到我的目标是 info_development.plist 而不是生产文件 info.plist
之前,我无法弄清楚为什么这对我不起作用如果您像我一样碰巧有多个 Plist(一个用于开发,一个用于生产等),请确保在任何地方都对其进行编辑。 ;-)
Swift 5
这些答案可以打开gmail 但如果用户没有在设备上安装gmail 怎么办?在那种情况下,我已经处理了打开苹果 mail/outlook/yahoo/spark。如果存在 none 个,我将显示警报。
@IBAction func openmailAction() {
if let googleUrl = NSURL(string: "googlegmail://") {
openMail(googleUrl)
} else if let mailURL = NSURL(string: "message://") {
openMail(mailURL)
} else if let outlookURL = NSURL(string: "ms-outlook://") {
openMail(outlookURL)
} else if let yahooURL = NSURL(string: "ymail://") {
openMail(yahooURL)
} else if let sparkUrl = NSURL(string: "readdle-spark://") {
openMail(sparkUrl)
} else {
// showAlert
}
}
func openMail(_ url: NSURL) {
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
}
您可能还需要将此添加到 plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlegmail</string>
<string>ms-outlook</string>
<string>readdle-spark</string>
<string>ymail</string>
</array>