以编程方式打开iPhone的邮件客户端

use*_*089 15 email iphone gmail

在我的应用程序中,如果用户提供了他们的Gmail帐户,那么我需要使用gmail登录凭据打开邮件客户端,当我们以编程方式选择邮件的gmail选项时,如果该帐户已经存储在邮件中,那么我需要重定向用户直接到他们的帐户.任何人都可以给我一瞥如何以编程方式实现这一目标.

Léo*_*urg 42

您不会对Mail应用程序有太多控制权,因为iPhone上的所有应用程序都是沙箱,以防止它们弄乱Apple应用程序.

您唯一可以做的事情(如果您想打开邮件客户端发送电子邮件),就像这样:

/* create mail subject */
NSString *subject = [NSString stringWithFormat:@"Subject"];

/* define email address */
NSString *mail = [NSString stringWithFormat:@"test@test.com"];

/* define allowed character set */
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet];

/* create the URL */
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"mailto:?to=%@&subject=%@",
                                                                                        [mail stringByAddingPercentEncodingWithAllowedCharacters:set],
                                                                                        [subject stringByAddingPercentEncodingWithAllowedCharacters:set]]];    
/* load the URL */
[[UIApplication sharedApplication] openURL:url];

/* release the URL. If you are using ARC, remove this line. */
[url release];
Run Code Online (Sandbox Code Playgroud)


Ale*_*kov 5

迅速:

        if let url = NSURL(string: "mailto://\(email)") {
            UIApplication.sharedApplication().openURL(url)
        }
Run Code Online (Sandbox Code Playgroud)


Jer*_*iah 5

Léon Rodenburg 答案的 Swift 版本:

    // define email address
    let address = "test@test.com"

    // create mail subject
    let subject = "Subject"

    // create the URL
    let url = NSURL(string: "mailto:?to=\(address)&subject=\(subject)".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)

    // load the URL
    UIApplication.sharedApplication().openURL(url!)
Run Code Online (Sandbox Code Playgroud)


Joh*_*ger 5

我会建议一个更加改进的答案。Slack.com 移动应用程序可以执行此操作,它会检测设备上列出的常见电子邮件客户端,并显示您要打开的“哪个”电子邮件客户端的弹出选择器。

因此要实施:

  1. 通过 Google 查找排名前 10 位的电子邮件客户端(例如 Mail、Google Inbox、OutLook、AirMail 等)。

  2. 通过搜索所有应用程序来获取手机上已安装应用程序的列表(但我被告知您现在只能查找是否明确安装了应用程序,因此您需要检测该应用程序)。

  3. 如果检测到超过 1 个电子邮件应用程序,则显示弹出列表,请求他们打开“哪个”应用程序,例如。邮件、收件箱。

这是迄今为止我见过的最好的解决方案。