MFMailComposeViewController dismissed very slow after clicked send button

97 views Asked by At

I have a bit of a strange problem. I am trying to send in-app email. So, I use MFMailComposeViewController for that.And I have realized MFMailComposeViewControllerDelegate. When I click the send button, it should be dismissed immediately.In debug mode,it works well,BUT,in release mode,it is extremely slow so that user may click send button more than one time and the mail will be sent more than one time. I have been confused for five days. Anyone can help me ? My code:

-(void)sendEMail
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

    if (mailClass != nil)
    {
        if ([mailClass canSendMail])
        {
            [self displayComposerSheet];
            //[self launchMailAppOnDevice];
        } else{
            [self launchMailAppOnDevice];
        }
    } else {
        [self launchMailAppOnDevice];
    }
}
//可以发送邮件的话
-(void)displayComposerSheet
{
    //mailPicker = [[MFMailComposeViewController alloc] init];

    mailPicker.mailComposeDelegate = self;

    //设置主题
    [mailPicker setSubject: kEmailSubject];

    // 添加发送者
    NSArray *toRecipients = [NSArray arrayWithObject: kToRecipient];
    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
    //NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]", nil];
    [mailPicker setToRecipients: toRecipients];
    //[picker setCcRecipients:ccRecipients];
    //[picker setBccRecipients:bccRecipients];

    // 添加图片
   // UIImage *addPic = [UIImage imageNamed: @"123.jpg"];
   // NSData *imageData = UIImagePNGRepresentation(addPic);            // png
    // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg
   // [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];
    NSString *emailBody = kEmailBody;
    [mailPicker setMessageBody:emailBody isHTML:YES];
    [self presentViewController:mailPicker animated:YES completion:nil];
}
-(void)sendMailBtnCLicked{
    [mailPicker dismissViewControllerAnimated:YES completion:nil];
}
-(void)launchMailAppOnDevice
{
    NSString *recipients = kToRecipient;
    NSString *subject = kEmailSubject;
    //@"mailto:[email protected][email protected],[email protected]&subject=my email!";
    NSString *body = kEmailBody;

    NSString *email = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@", recipients, subject,body];
    email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    if (result == MFMailComposeResultSent) {
        [AccountManager sharedManager].isSentMail = YES;
    }
    [controller dismissViewControllerAnimated:YES completion:nil];
    //[self dismissModalViewControllerAnimated:YES];
}
1

There are 1 answers

0
lihk11 On

Stop other threads running background. After the mail sent, restart them.