我需要在用户使用MFMailComposeViewController从iPhone成功发送邮件时显示警告消息。
我尝试使用didFinishWithResult委托,但它同时调用send和cancel,那么我们如何确定我们成功发送消息?
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的短信和两个didFinishWithResult例程使用类似的方法与上面的一个,在VC被驳回之前显示一个警告。
似乎如果你发送了一条短信,下次你尝试发送电子邮件时,光标不会出现在电子邮件正文中,你也不能选择任何文本。同样在调试器中,我得到"wait_fences: failed to receive reply: 10004003"。
我最终只是从应用程序的这一部分删除了警报视图,问题就消失了。如果有人有解决这个问题的办法,我很乐意听听。
你应该为委托对象实现这个方法…
– mailComposeController:didFinishWithResult:error:
查看这个以获得更多细节…http://developer.apple.com/library/ios/文档/MessageUI/引用/MFMailComposeViewControllerDelegate_protocol/引用/Reference.html #//apple_ref/occ/intf MFMailComposeViewControllerDelegate
(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
使用这个委托,在这个委托中MFMailComposeResult是一个enum
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.")
}