Open Gmail app from my app

Ankita Shah picture Ankita Shah · Aug 20, 2015 · Viewed 13.3k times · Source

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.

Answer

ghashi picture ghashi · Oct 19, 2016

Setup for iOS9+

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

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