代码之家  ›  专栏  ›  技术社区  ›  Ankita Shah

从我的应用程序打开Gmail应用程序

  •  27
  • Ankita Shah  · 技术社区  · 11 年前

    我正在尝试从我的应用程序发送电子邮件。但我希望的是,若用户的手机上有Gmail应用程序,那个么邮件应该使用它发送。如果Gmail应用程序不可用,则应将用户重定向到邮箱。

    所以,我如何知道用户是否包含Gmail应用程序,以及如何将用户重定向到该应用程序。

    5 回复  |  直到 11 年前
        1
  •  40
  •   Community Mohan Dere    9 年前

    iOS9的设置+

    如前所述 here , 如果您使用iOS9+ ,不要忘记添加 googlegmail 到 LSApplicationQueriesSchemes 在您的 info.plist

    my 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)
            }
        }
    }
    
        2
  •  31
  •   Grzegorz Krukowski    11 年前

    您需要使用自定义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
    }  
    
        3
  •  3
  •   biomiker    7 年前

    对于 斯威夫特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")
      }
      
        4
  •  1
  •   Yasper    7 年前

    直到我意识到我的目标是信息开发,我才明白为什么这对我不起作用。plist而不是生产文件info.plist

    如果你像我一样,碰巧有多个Plist(一个用于开发,一个用于生产等),请确保在任何地方都编辑它

        5
  •  1
  •   Naval Hasan    4 年前

    雨燕5

    这些答案可以打开gmail,但如果用户没有在设备中安装gmail怎么办?在这种情况下,我已经处理了打开applemail/outlook/yahoo/spark。如果他们都不在场,我会发出警报。

    @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>