Open Gmail app from my app

后端 未结 4 629
长发绾君心
长发绾君心 2020-12-24 02:30

I\'m trying to send an email from my app. But what I want is if user is having Gmail app on his/her phone, then mail should be sent using it. If Gmail app is unavailable the

相关标签:
4条回答
  • 2020-12-24 03:01

    I couldn't figure out why this wasn't working for me until I realised I was targetting a info_development.plist instead of the production-file info.plist

    If you're like me and happen to have multiple Plists (one for development, one for prod etc) make sure you edit it everywhere. ;-)

    0 讨论(0)
  • 2020-12-24 03:02

    Setup for iOS9+

    As explained here, if you're on iOS9+, don't forget to add googlegmail to LSApplicationQueriesSchemes on your info.plist

    Code to open GMail

    Then, you can do the same as the accepted answer (below is my swift 2.3 version):

    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)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-24 03:05

    You need to use custom URL Scheme. For gmail application its:

    googlegmail://
    

    If you want to compose a message there you can add more parameters to this URL:

    co?subject=Example&body=ExampleBody
    

    You can determinate if any kind of application is installed using this code (just replace customURL obviously for an other apps):

    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
    }  
    
    0 讨论(0)
  • 2020-12-24 03:09

    For Swift 3.0+

    Notes:

    1. This solution shows how to use spaces or newlines in the arguments to the URL (Gmail may not respect the newlines).
    2. It is NOT necessary to register with LSApplicationQueriesSchemes as long as you don't call canOpenURL(url). Just try and use the completion handler to determine if it succeeded.

      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")
      }
      
    0 讨论(0)
提交回复
热议问题