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

发布于 2024-12-21 23:41:06 字数 152 浏览 0 评论 0原文

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

In my application, if the user gave their gmail account then i am required to open the mail client with the gmail login credentials which comes when we select gmail option of mail programmatically but if that account is already stored in mail then i am required to redirect the user directly to their account. Can anybody pliz give me a glimpse of how i can achieve this programmatically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅浅 2024-12-28 23:41:06

你不会对邮件应用程序有太多的控制权,因为 iPhone 上的所有应用程序都经过沙箱处理,以防止它们干扰 Apple 应用程序。

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

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

/* define email address */
NSString *mail = [NSString stringWithFormat:@"[email protected]"];

/* 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];

You won't get that much control over the Mail app as all apps on the iPhone are sandboxed to prevent them from messing with Apple applications.

The only thing you can do (if you want to open the mail client to send an email), is something like this:

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

/* define email address */
NSString *mail = [NSString stringWithFormat:@"[email protected]"];

/* 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];
你怎么这么可爱啊 2024-12-28 23:41:06

Léon Rodenburg 答案的 Swift 版本:

    // define email address
    let address = "[email protected]"

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

Swift version of Léon Rodenburg's answer:

    // define email address
    let address = "[email protected]"

    // 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!)
や莫失莫忘 2024-12-28 23:41:06

迅速:

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

Swift:

        if let url = NSURL(string: "mailto://\(email)") {
            UIApplication.sharedApplication().openURL(url)
        }
青巷忧颜 2024-12-28 23:41:06

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

因此要实施:

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

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

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

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

I would suggest a much more improved answer.
The Slack.com mobile app does this, it detects common email clients listed on the device and shows a popup picker of 'which' email client you would like to open.

So to implement:

  1. Google around to find the top 10 email clients (eg Mail, Google Inbox, OutLook, AirMail etc).

  2. Get a list of installed apps on the phone either by searching all apps (but I am told you can now only find if an app is explicitly installed, so you will need detect the app).

  3. Show a popup list if more than 1 email app is detected, requesting them 'which' app to open eg. Mail, Inbox.

This is the best solution I have seen working to date.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文