Open Gmail app from my app

陌路散爱 提交于 2019-11-30 11:03:58

问题


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 then the user should be redirected to Mailbox.

So how can I know if user contains Gmail app and how can I redirect user to it.


回答1:


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
}  



回答2:


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



回答3:


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")
    }
    



回答4:


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. ;-)



来源:https://stackoverflow.com/questions/32114455/open-gmail-app-from-my-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!