Ankita Shah Ankita Shah - 1 month ago 45
Swift Question

Open Gmail app from my app

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.

Thanks in advance

Answer

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