0

在我的 iPhone 应用程序中。我正在使用MFMailComposeViewController. 现在,当我第二次进入邮件表时,我的应用程序崩溃了。我在谷歌搜索。但我找不到解决方案。任何人都可以帮助我解决我的问题。请查看下面的代码并帮助我我在哪里做错了。

if ([MFMailComposeViewController canSendMail])
        {
            controller = [[MFMailComposeViewController alloc] init];
            controller.mailComposeDelegate = self;
            [controller setSubject:@""];
            [controller setToRecipients:array1];
            [controller setMessageBody:@"" isHTML:NO];
            [controller setMailComposeDelegate: self];
            [controller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            [self.navigationController presentModalViewController:controller animated:NO];
            [controller release];
        }

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
    if(result == MFMailComposeResultSent)
    {
        [[self parentViewController] dismissModalViewControllerAnimated:YES];
    }
    else if (result == MFMailComposeResultCancelled)
    {
        [[self parentViewController] dismissModalViewControllerAnimated:YES];
    }
 }
4

6 回答 6

0

在您打开邮件视图的方法中,用下面的代码替换您的代码。告诉我它是否有效!!!!!!编码快乐!!!!

 MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

if ([MFMailComposeViewController canSendMail] == NO) {
        return;
}
else
{
    picker.mailComposeDelegate = self;
    NSString *stringtitle=[NSString stringWithFormat:@"Apple"];
    [picker setSubject:stringtitle];

    NSData *imgDataLoop = UIImagePNGRepresentation(shaereImage.image);

    [picker addAttachmentData:imgDataLoop mimeType:@"image/png" fileName:@"rainy"];

    [self presentViewController:picker animated:YES completion:nil];
    [picker release];

}
于 2012-12-11T05:27:21.783 回答
0
-(void)mail
{ 
  if ([MFMailComposeViewController canSendMail])
    {

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

    [mailController setSubject:@"your Subject"];
    [mailController setMessageBody:@"your message" isHTML:NO];

    UIImage *tmp = [UIImage imageNamed:@"Icon.png"];
    NSData *myData = UIImageJPEGRepresentation(tmp, 1.0);

    [mailController addAttachmentData:myData mimeType:@"image/png" fileName:@"MyPhoto.png"];
    if (arrTo)
        [mailController setToRecipients:arrTo];

    [self presentModalViewController:mailController animated:YES];
    return YES;
}
else {
    return NO;
}
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{   

    [controller dismissModalViewControllerAnimated:YES];
}
于 2012-12-12T04:56:09.910 回答
0

Change your methods to,

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"sent");
            [self showErrorMessage: @"Email sent successfully"];
            break;
        case MFMailComposeResultFailed:
            NSLog(@"failed");
            [self showErrorMessage: @"Failed to send email"];
            break;
        default:
            NSLog(@"not sent");
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];

}

-(void)showErrorMessage:(NSString *)iMessage
{
        NSLog(@"%@", iMessage);
}

As I mentioned earlier, your method signature was different while calling and in the definition part. It should be the same at both places as shown above.

While calling the method was [self showErrorMessage: @"Failed to send email"]; but in definition you were not accepting the string part. You need to have an input param for the same. You can also do the same by calling it as [self showErrorMessage]; and then keep the showErrorMessage method as it is.

于 2012-12-11T06:19:31.560 回答
0

我认为这段代码会导致问题:

[[self parentViewController] dismissModalViewControllerAnimated:YES];

而不是上述使用:

[self dismissModalViewControllerAnimated:YES];
于 2012-12-11T04:58:46.257 回答
0

尝试编写此委托方法,而不是编写相同的方法

让我知道它是否有效!!!!

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"canceled");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"saved");
        break;
    case MFMailComposeResultSent:
        NSLog(@"sent");
        [self showErrorMessage: @"Email sent successfully"];
        break;
    case MFMailComposeResultFailed:
        NSLog(@"failed");
        [self showErrorMessage: @"Failed to send email"];
        break;
    default:
        NSLog(@"not sent");
        break;
}
[self dismissViewControllerAnimated:YES completion:nil];

}

编码快乐!!!!!!!

于 2012-12-11T05:07:35.517 回答
0

替换此代码:

[self.navigationController presentModalViewController:controller animated:NO];

和:

[self presentViewController:controller animated:YES completion:nil];
于 2012-12-11T05:11:02.973 回答