如何在iPhone上以编程方式显示邮件设置页面?

7

我该如何通过程序显示邮件设置页面?

在我的应用中,我为用户提供了反馈选项。当点击反馈按钮时,我会检查设备中是否有任何可用的邮件帐户。这是通过以下检查完成的:

if ([MFMailComposeViewController canSendMail])
{
    // Actions to send mail
}
else
{
    //Actions to show an error message by UIAlertView
}

警告消息将会是这样的: 无邮件账号警告 如果用户在这个UIAlertView中点击了OK按钮,我想要跳转到设置菜单中可用的邮件设置页面。也就是说,我想要显示下面的页面: 邮件设置页面 有没有可能通过编程实现这种导航呢?
6个回答

6
当用户点击警告视图的“确定”按钮时,请使用以下代码。
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"mailto:test@test.com"]];

这将打开本机邮件应用程序主页,允许用户添加新的邮件帐户。

希望对您有所帮助 :)


但首先要测试以确保URL可以打开。用户可能无法在启用了家长控制的情况下打开邮件。在这种情况下,Rajkanth真的会陷入困境。 - fearmint
2
只需对代码进行小修正即可使其正常工作 - [[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"mailto:test@test.com"]] - Kostiantyn Sokolinskyi

2
您需要使用MFMailComposeViewController类和MFMailComposeViewControllerDelegate协议,
PeyloW在他的答案here中提供了以下代码:

首先发送一条消息:

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"My Subject"];
[controller setMessageBody:@"Hello there." isHTML:NO]; 
[self presentModalViewController:controller animated:YES];
[controller release];

然后用户完成工作,您及时收到委托回调:
- (void)mailComposeController:(MFMailComposeViewController*)controller  
          didFinishWithResult:(MFMailComposeResult)result 
                        error:(NSError*)error;
{
  if (result == MFMailComposeResultSent) {
    NSLog(@"It's away!");
  }
  [self dismissModalViewControllerAnimated:YES];
}

感谢您的回复...这是使用MFMailComposeViewController发送邮件的过程。当您之前通过“设置”->“邮件、联系人、日历”->“添加帐户”设置了帐户时,它将起作用。如果您在设备上没有设置帐户,则无法正常工作。我是对的吗?因此,我们应该向用户显示某些消息,以指示使用UIAlertView设置邮件帐户。我的意思是,在显示UIAlertView中的消息后,如果用户点击警报视图的OK按钮,则我上面提到的设置页面应自动出现在用户面前。 - Rajkanth
如果您的设备上尚未设置邮件帐户,则在允许您发送消息之前,它将显示邮件设置视图(据我所知)。 - Thomas Clayson
你真的应该指明你代码的来源是 PeyloW 在这里的回答:https://dev59.com/gXVC5IYBdhLWcg3wYQEp#1513433 - Brad Larson

0
在.h文件中添加messageUI框架。
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
 add <MFMailComposeViewControllerDelegate> like
@interface ManageRequestViewController : UIViewController<MFMailComposeViewControllerDelegate>

in .m file

if([MFMailComposeViewController canSendMail]){

            MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
            mail.mailComposeDelegate=self;
            [mail setSubject:@"your subject"];          
            [mail setMessageBody:@"mail!" isHTML:NO];
            [self presentModalViewController:mail animated:YES];
            [mail release];         
        }

- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
        [self dismissModalViewControllerAnimated:YES];

}

这是用于发送邮件的代码... 我的问题是如何从设置中显示邮件帐户设置页面。 - Rajkanth

0

对于你的具体问题,简短的回答是:无法使用 iOS SDK 编程实现邮件帐户的创建。


我不想以编程方式添加帐户。我只想显示设置页面。这样,用户就不需要手动去那里了。 - Rajkanth

0

无法完成。即使有一个启动设置应用程序的接口(我不知道是否有),也没有办法指定要转到该应用程序的哪个屏幕。这不像网站,每个页面都有一个URL。


0

-(IBAction)showPicker:(id)sender {

-(IBAction)显示选择器:(id)发送者 {

    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)

    {

        // We must always check whether the current device is configured for sending emails

        if ([mailClass canSendMail])
        {

            [self displayComposerSheet];

        }
        else

        {
            [self launchMailAppOnDevice];
        }
    }
    else
    {
        //mail not config
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接