【发布时间】:2015-11-13 20:55:28
【问题描述】:
我正在尝试从我的应用程序发送电子邮件。但我想要的是,如果用户在他/她的手机上安装了 Gmail 应用程序,那么应该使用它发送邮件。如果 Gmail 应用程序不可用,则应将用户重定向到邮箱。
那么我如何知道用户是否包含 Gmail 应用程序以及如何将用户重定向到它。
【问题讨论】:
标签: ios uiapplication
我正在尝试从我的应用程序发送电子邮件。但我想要的是,如果用户在他/她的手机上安装了 Gmail 应用程序,那么应该使用它发送邮件。如果 Gmail 应用程序不可用,则应将用户重定向到邮箱。
那么我如何知道用户是否包含 Gmail 应用程序以及如何将用户重定向到它。
【问题讨论】:
标签: ios uiapplication
直到我意识到我的目标是 info_development.plist 而不是生产文件 info.plist,我才弄清楚为什么这对我不起作用
如果您像我一样并且碰巧有多个 Plist(一个用于开发,一个用于生产等),请确保您在任何地方都对其进行编辑。 ;-)
【讨论】:
对于 Swift 3.0+
注意事项:
只要您不调用 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")
}
【讨论】:
正如here 解释的那样,如果您使用的是 iOS9+,请不要忘记在您的info.plist 上将googlegmail 添加到LSApplicationQueriesSchemes
然后,你可以按照接受的答案做同样的事情(下面是我的 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)
}
}
}
【讨论】:
to 参数:"googlegmail:///co?to=support@test.com&subject=Hello&body=Hi"
%20(空格)和%0D%0A(新行)...会是这样的:googlegmail:///co?to=support@test.com&subject=Hello&body=Text%20before%20new%20line.%0D%0AText%20after%20new%20line....查看此链接:@987654323 @
canOpenURL 工作,我需要将googlegmail 添加到plist。在 iOS 10+ 上。
您需要使用自定义 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
}
【讨论】:
"googlegmail:///co?to=support@test.com&subject=Hello&body=Hi"
googlegmail:///co? 和 googlegmail://co?,都在 iOS 12.1.2 + Gmail 5.0.190113 上运行