【发布时间】:2017-12-13 00:54:08
【问题描述】:
当用户使用 MFMailComposeViewController 从 iPhone 成功发送邮件时,我需要显示警报消息。
我尝试使用 didFinishWithResult 委托,但它同时调用了发送和取消,那么我们如何确定我们成功发送了消息?
【问题讨论】:
标签: iphone email mfmailcomposeviewcontroller
当用户使用 MFMailComposeViewController 从 iPhone 成功发送邮件时,我需要显示警报消息。
我尝试使用 didFinishWithResult 委托,但它同时调用了发送和取消,那么我们如何确定我们成功发送了消息?
【问题讨论】:
标签: iphone email mfmailcomposeviewcontroller
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];
}
【讨论】:
我在使用这种方法时遇到了麻烦。在我的应用程序中,我将 MFMailComposeViewController 用于电子邮件,将 MFMessageComposeViewController 用于 SMS 消息,并且两个 didFinishWithResult 例程都使用了与上述方法类似的方法,其中在 VC 关闭之前显示警报。
似乎如果您发送短信,下次您尝试发送电子邮件时,光标将不会出现在电子邮件正文中,并且您无法选择任何文本。同样在调试器中,我收到“wait_fences: failed to receive reply: 10004003”。
我最终只是从应用程序的这一部分中删除了警报视图,问题就消失了。如果有人对此问题有解决方案,我会很高兴听到。
【讨论】:
你应该为委托对象实现这个方法...
– mailComposeController:didFinishWithResult:error:
【讨论】:
(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
使用这个委托,在这个 MFMailComposeResult 里面是一个枚举
enum MFMailComposeResult {
MFMailComposeResultCancelled,
MFMailComposeResultSaved,
MFMailComposeResultSent,
MFMailComposeResultFailed
};
typedef enum MFMailComposeResult MFMailComposeResult;
【讨论】:
答案的 Swift 版本。
func sendMail(){
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["hello@gmail.com"])
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.")
}
【讨论】: