【问题标题】:Open Gmail app from my app从我的应用打开 Gmail 应用
【发布时间】:2015-11-13 20:55:28
【问题描述】:

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

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

【问题讨论】:

标签: ios uiapplication


【解决方案1】:

直到我意识到我的目标是 info_development.plist 而不是生产文件 info.plist,我才弄清楚为什么这对我不起作用

如果您像我一样并且碰巧有多个 Plist(一个用于开发,一个用于生产等),请确保您在任何地方都对其进行编辑。 ;-)

【讨论】:

    【解决方案2】:

    对于 Swift 3.0+

    注意事项:

    1. 此解决方案展示了如何在 URL 的参数中使用空格或换行符(Gmail 可能不尊重换行符)。
    2. 只要您不调用 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")
      }
      

    【讨论】:

      【解决方案3】:

      iOS9+ 设置

      正如here 解释的那样,如果您使用的是 iOS9+,请不要忘记在您的info.plist 上将googlegmail 添加到LSApplicationQueriesSchemes

      打开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)
              }
          }
      }
      

      【讨论】:

      • iOS 11:它无需向您自己的应用程序添加方案即可工作。您也可以添加to 参数:"googlegmail:///co?to=support@test.com&subject=Hello&body=Hi"
      • @silvansky,你知道如何传递多行正文吗?我的意思是,包括新线?所以,不是“嗨”而是“嗨!”然后在新行上“再见”。
      • 如何在正文中添加空格?
      • 您是否尝试过使用%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+ 上。
      【解决方案4】:

      您需要使用自定义 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
      }  
      

      【讨论】:

      • 你能定义一个附加图片的方法吗?
      • 它工作得很好,但是如果我想添加“To:”的参数怎么办?意味着我如何添加我想要发送邮件的电子邮件 ID,就像你添加主题和正文一样?
      • @YuvrajSinh 你可以像这样添加它:"googlegmail:///co?to=support@test.com&subject=Hello&body=Hi"
      • googlegmail:///co?googlegmail://co?,都在 iOS 12.1.2 + Gmail 5.0.190113 上运行
      猜你喜欢
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多