当用户使用MFMailComposeViewController从iPhone发送成功邮件时,我需要显示警告消息。
我尝试使用didFinishWithResult委托但是它要求发送和取消然后我们如何确定我们成功发送消息?
答案 0 :(得分:12)
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];
}
答案 1 :(得分:2)
我遇到了这种方法的麻烦。在我的应用程序中,我使用MFMailComposeViewController作为电子邮件,MFMessageComposeViewController用于SMS消息,并且didFinishWithResult例程使用了与上述类似的方法,其中在VC被解除之前显示警报。
如果您发送短信,下次您尝试发送电子邮件时,光标将不会显示在电子邮件正文中,您也无法选择任何文字。同样在调试器中我得到“wait_fences:未能收到回复:10004003”。
我最终刚从应用程序的这一部分删除了警报视图,问题就消失了。如果有人对此问题有解决方案,我会很高兴听到它。
答案 2 :(得分:1)
你应该为委托对象实现这个方法......
– mailComposeController:didFinishWithResult:error:
答案 3 :(得分:1)
(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
使用此委托,在此MFMailComposeResult内部是一个枚举
enum MFMailComposeResult {
MFMailComposeResultCancelled,
MFMailComposeResultSaved,
MFMailComposeResultSent,
MFMailComposeResultFailed
};
typedef enum MFMailComposeResult MFMailComposeResult;
答案 4 :(得分:1)
答案的快速版本。
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.")
}