【问题标题】:How to use MFMailComposerViewController on iOS 7?如何在 iOS 7 上使用 MFMailComposeViewController?
【发布时间】:2013-09-24 10:16:42
【问题描述】:

我正在为 iOS 7 开发应用程序并使用 MFMailComposerViewController

我已经尝试了所有方法,但 dismissViewController:withAnimated 不起作用。

有时类在第一次使用presentViewCOntroller:withAnimated:completion 方法显示viewController 时会自动调用委托。

我的应用是基于导航的,这就是为什么我认为问题也与UINavigationController 相关。

       -(void)sendMail{

if ([MFMailComposeViewController canSendMail]) {

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    [picker setSubject:@"Hello from California!"];

    // Set up recipients
    NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
    NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];

    [picker setToRecipients:toRecipients];
    [picker setCcRecipients:ccRecipients];
    [picker setBccRecipients:bccRecipients];



    // Fill out the email body text
    NSMutableString *emailBody =[NSMutableString stringWithString: @"<table border=1 align=\"center\"><tr><th>EventDate</th><th>EventDay</th><th>EventTime</th><th>Speaker</th><th>topic</th></tr>"];

    for (int i=0; i<5; i++) {
        NSString *eventDate=[NSString stringWithFormat:@"<tr><td>%@</td>",@"12/11"];
        NSString *eventDay=[NSString stringWithFormat:@"<td>%@</td>",@"Sunday"];
        NSString *eventTime=[NSString stringWithFormat:@"<td>%@</td>",@"12:10 pm"];
        NSString *eventSpeaker=[NSString stringWithFormat:@"<td>%@</td>",@"RajVeer"];
        NSString *eventTopic=[NSString stringWithFormat:@"<td>%@</td>",@"nano-technology"];
        NSString *dataString=[NSString stringWithFormat:@"%@%@%@%@%@</tr>",eventDate,eventDay,eventTime,eventSpeaker,eventTopic];
        [emailBody appendString:dataString];
    }
    NSString *lastTable=@"</table>";
    [emailBody appendString:lastTable];
    NSLog(@"%@",emailBody);
    [picker setMessageBody:emailBody isHTML:YES];

    [self presentViewController:picker animated:YES completion:NULL];


}

【问题讨论】:

  • 向我们展示您用于呈现和关闭视图控制器的代码。
  • 在 iOS 7 上,它的工作方式与在早期版本的 iOS 上的工作方式相同(刚刚测试过)。你的代码应该有问题。

标签: ios cocoa-touch ios7 mfmailcomposeviewcontroller


【解决方案1】:

这应该可以解决问题:

#pragma mark MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [controller dismissViewControllerAnimated:YES completion:nil];
}

【讨论】:

    【解决方案2】:

    使用此代码展示MFMailComposeViewController

    [self presentViewController:mailComposerObject animated:YES completion:NULL];
    

    对于解雇MFMailComposeViewController

    #pragma mark - MFMailComposeViewControllerDelegate
    -(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError*)error {
    [self dismissViewControllerAnimated:YES completion:NULL];
    }
    

    从 iOS 6.0 开始,[self presentModalViewController:&lt;#(UIViewController *)#&gt; animated:&lt;#(BOOL)#&gt;] 已弃用。

    【讨论】:

    • [selfdismissViewControllerAnimated:YES 完成:NULL];不适用于 iOS 7
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    相关资源
    最近更新 更多