iPhone 中从 MFMailComposeViewController 发送邮件时显示成功提醒消息

发布于 2024-12-19 11:35:30 字数 136 浏览 2 评论 0原文

当用户使用 MFMailComposeViewController 从 iPhone 成功发送邮件时,我需要显示警报消息。

我尝试使用 didFinishWithResult 委托,但它同时要求发送和取消,那么我们如何确定我们成功发送了消息?

I need to display the alert message when the user send successfully mail from the iPhone using MFMailComposeViewController.

I tried with didFinishWithResult delegate but it is calling for both send and cancel then how we can determine we successfully send the message?

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

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

发布评论

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

评论(5

和我恋爱吧 2024-12-26 11:35:30
Try this code

-(IBAction)Btn_EmailPressed:(id)sender{
    if (![MFMailComposeViewController canSendMail]) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Email cannot be configure." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }else {
        picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate=self;
        [picker setToRecipients:nil];
                [picker setSubject:@"Email"];
                [picker setMessageBody:nil isHTML:NO];
                NSArray *toRecipients = [[NSArray alloc] initWithObjects:lblSite.text,nil];
                [picker setToRecipients:toRecipients];
                [self presentModalViewController:picker animated:YES];
            }
}


- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    NSString *msg1;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            msg1 =@"Sending Mail is cancelled";
            break;
        case MFMailComposeResultSaved:
            msg1=@"Sending Mail is Saved";
            break;
        case MFMailComposeResultSent:
            msg1 =@"Your Mail has been sent successfully";
            break;
        case MFMailComposeResultFailed:
            msg1 =@"Message sending failed";
            break;
        default:
            msg1 =@"Your Mail is not Sent";
            break;
    }
    UIAlertView *mailResuletAlert = [[UIAlertView alloc]initWithFrame:CGRectMake(10, 170, 300, 120)];
    mailResuletAlert.message=msg1;
    mailResuletAlert.title=@"Message";
    [mailResuletAlert addButtonWithTitle:@"OK"];
    [mailResuletAlert show];
    [mailResuletAlert release];
    [self dismissModalViewControllerAnimated:YES];  
}
Try this code

-(IBAction)Btn_EmailPressed:(id)sender{
    if (![MFMailComposeViewController canSendMail]) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Email cannot be configure." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }else {
        picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate=self;
        [picker setToRecipients:nil];
                [picker setSubject:@"Email"];
                [picker setMessageBody:nil isHTML:NO];
                NSArray *toRecipients = [[NSArray alloc] initWithObjects:lblSite.text,nil];
                [picker setToRecipients:toRecipients];
                [self presentModalViewController:picker animated:YES];
            }
}


- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    NSString *msg1;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            msg1 =@"Sending Mail is cancelled";
            break;
        case MFMailComposeResultSaved:
            msg1=@"Sending Mail is Saved";
            break;
        case MFMailComposeResultSent:
            msg1 =@"Your Mail has been sent successfully";
            break;
        case MFMailComposeResultFailed:
            msg1 =@"Message sending failed";
            break;
        default:
            msg1 =@"Your Mail is not Sent";
            break;
    }
    UIAlertView *mailResuletAlert = [[UIAlertView alloc]initWithFrame:CGRectMake(10, 170, 300, 120)];
    mailResuletAlert.message=msg1;
    mailResuletAlert.title=@"Message";
    [mailResuletAlert addButtonWithTitle:@"OK"];
    [mailResuletAlert show];
    [mailResuletAlert release];
    [self dismissModalViewControllerAnimated:YES];  
}
吹梦到西洲 2024-12-26 11:35:30

我对这种方法遇到了麻烦。在我的应用程序中,我使用 MFMailComposeViewController 处理电子邮件,使用 MFMessageComposeViewController 处理短信,两个 didFinishWithResult 例程都使用与上述方法类似的方法,在 VC 关闭之前显示警报。

似乎如果您发送短信,下次尝试发送电子邮件时,光标将不会出现在电子邮件正文中,并且您无法选择任何文本。另外,在调试器中,我收到“wait_fences:无法接收回复:10004003”。

我最终只是从应用程序的这一部分删除了警报视图,问题就消失了。如果有人对这个问题有解决方案,我很高兴听到。

I had trouble with this approach. In my app I used MFMailComposeViewController for email and MFMessageComposeViewController for SMS messages and both didFinishWithResult routines used a similar approach to the one above, where an alert is shown before the VC is dismissed.

It seemed that if you sent an SMS, the next time you tried an email the cursor would not appear in the email body and you could not select any text. Also in the debugger I was getting "wait_fences: failed to receive reply: 10004003".

I eventually just removed the alert views from this part of the app and the problem went away. If anyone has a resolution for this issue I'd be glad to hear it.

满地尘埃落定 2024-12-26 11:35:30
(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

使用此委托,并且此 MFMailComposeResult 内部是一个枚举

enum MFMailComposeResult {
   MFMailComposeResultCancelled,
   MFMailComposeResultSaved,
   MFMailComposeResultSent,
   MFMailComposeResultFailed
};
typedef enum MFMailComposeResult MFMailComposeResult;
(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

Use this delegate and inside this MFMailComposeResult is an enum

enum MFMailComposeResult {
   MFMailComposeResultCancelled,
   MFMailComposeResultSaved,
   MFMailComposeResultSent,
   MFMailComposeResultFailed
};
typedef enum MFMailComposeResult MFMailComposeResult;
长途伴 2024-12-26 11:35:30

答案的快速版本。

       func sendMail(){
                if MFMailComposeViewController.canSendMail() {
                    let mail = MFMailComposeViewController()
                    mail.mailComposeDelegate = self
                    mail.setToRecipients(["[email protected]"])
                    present(mail, animated: true)
                } else {
                    showSendMailErrorAlert()
                }
            }

        func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            switch result {
                case .cancelled: print("Sending Mail is cancelled")
                case .sent : print("Your Mail has been sent successfully")
                case .saved : print("Sending Mail is Saved")
                case .failed : print("Message sending failed")
            }
            controller.dismiss(animated: true, completion: nil)
        }

        func showSendMailErrorAlert() {
            showAlert(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.")
        }

Swift Version of the Answer.

       func sendMail(){
                if MFMailComposeViewController.canSendMail() {
                    let mail = MFMailComposeViewController()
                    mail.mailComposeDelegate = self
                    mail.setToRecipients(["[email protected]"])
                    present(mail, animated: true)
                } else {
                    showSendMailErrorAlert()
                }
            }

        func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            switch result {
                case .cancelled: print("Sending Mail is cancelled")
                case .sent : print("Your Mail has been sent successfully")
                case .saved : print("Sending Mail is Saved")
                case .failed : print("Message sending failed")
            }
            controller.dismiss(animated: true, completion: nil)
        }

        func showSendMailErrorAlert() {
            showAlert(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.")
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文