繁体   English   中英

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

[英]Open mail client of iPhone programmatically

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

您不会对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];

迅速:

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

Swift版本的LéonRodenburg的回答:

    // 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!)

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

所以实施:

  1. 谷歌即将找到前10个电子邮件客户端(例如Mail,Google Inbox,OutLook,AirMail等)。

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

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

这是我见过的最好的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM