从我的应用中打开Gmail应用

Ank*_*hah 20 uiapplication ios

我正在尝试从我的应用发送电子邮件.但我想要的是如果用户在他/她的手机上使用Gmail应用程序,则应使用它发送邮件.如果Gmail应用不可用,则应将用户重定向到邮箱.

那么我怎么知道用户是否包含Gmail应用程序以及如何将用户重定向到该应用程序.

gha*_*shi 29

iOS9 +的安装程序

正如这里所解释的,如果您使用的是iOS9 +,请不要忘记添加googlegmailLSApplicationQueriesSchemes您的info.plist

我的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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • iOS 11:它可以不向您自己的应用添加方案。您还可以添加`to`参数:`“” googlegmail:/// co?to=support@test.com&subject=Hello&body=Hi“` (2认同)

Grz*_*ski 22

您需要使用自定义URL方案.对于gmail应用程序:

googlegmail://
Run Code Online (Sandbox Code Playgroud)

如果要在那里撰写邮件,可以向此URL添加更多参数:

co?subject=Example&body=ExampleBody
Run Code Online (Sandbox Code Playgroud)

您可以使用此代码确定是否安装了任何类型的应用程序(只需将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
}  
Run Code Online (Sandbox Code Playgroud)

  • @YuvrajSinh你可以像这样添加:`"googlegmail:/// co?to = support @ test.com&subject = hello&body = HI"` (4认同)