在我的应用程序中使用MFMailComposeViewControlle
r之后我遇到两个问题:
MFMailComposeViewController
后,我的初始UITextField
上的ViewController
文字已清除UIAlertController
以表示MFMailComposeResult
时,无法显示日志中的错误。问题1:这是我的MFMailComposeViewController
代码。这是UIAlertController
(UIAlertControllerStyleActionSheet
)的操作。
UIAlertAction *email = [UIAlertAction
actionWithTitle:@"Email the link"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// composes new mail message with link
NSString *messageBody = self.link.text;
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setMessageBody:messageBody isHTML:NO];
// customises UI to match application
[[mc navigationBar] setTintColor:[UIColor whiteColor]];
[self presentViewController:mc animated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setNeedsStatusBarAppearanceUpdate];
}];
[view dismissViewControllerAnimated:YES completion:nil];
}];
问题2:这是我为每个相关UIAlertController
显示相应MFMailComposeResult
的代码。
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:{
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:@"SearchMe"
message:@"The composition of this email has been cancelled."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *done = [UIAlertAction
actionWithTitle:@"Done"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:done];
[self presentViewController:alert animated:YES completion:nil];
[alert dismissViewControllerAnimated:YES completion:nil];
}
break;
case MFMailComposeResultSaved:{
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:@"SearchMe"
message:@"The composition of this email has saved as a draft."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *done = [UIAlertAction
actionWithTitle:@"Done"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:done];
[self presentViewController:alert animated:YES completion:nil];
[alert dismissViewControllerAnimated:YES completion:nil];
}
break;
case MFMailComposeResultSent:{
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:@"SearchMe"
message:@"The composition of this email has been sent."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *done = [UIAlertAction
actionWithTitle:@"Done"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:done];
[self presentViewController:alert animated:YES completion:nil];
[alert dismissViewControllerAnimated:YES completion:nil];
}
break;
case MFMailComposeResultFailed:
{
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:@"SearchMe"
message:@"The composition of this email has failed. Please try again."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *done = [UIAlertAction
actionWithTitle:@"Done"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:done];
[self presentViewController:alert animated:YES completion:nil];
[alert dismissViewControllerAnimated:YES completion:nil];
}
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
日志中给出的错误是:
Warning: Attempt to present <UIAlertController: 0x14ed58b90> on <ViewController: 0x14ee1f7f0> whose view is not in the window hierarchy!