У меня есть modalViewController с кнопкой под названием электронная почта. При нажатии на эту кнопку он представляет MFMailComposeViewController модально. Все работает хорошо, когда я отправляю письмо. Но когда пользователь нажимает кнопку отмены в представлении MFMail, приложение зависает и не отображает лист действий. Он не показывает никаких деталей в консоли и т. Д., Очевидно, в этом случае делегат MFMailCompose не вызывается.
Я проверил, используя тот же код в другом контроллере представления (который не был modalviewcontroller), и обнаружил, что все работает отлично там. Поэтому я предполагаю, что проблема заключается в представлении MFMailComposeViewController в контроллере, который уже является modalviewcontroller.
Я искал некоторую помощь довольно давно и пришел к выводу, что, поскольку MFMailComposeViewController сам по себе является modalviewcontroller, он может использоваться только на простом контроллере представления и не будет работать в modalviewcontroller.
Может кто-нибудь любезно подсказать мне, как использовать MFMailComposer в контроллере modalview. Любая помощь будет принята с благодарностью.
Мой код:
-(IBAction)emailButtonPressed:(id)sender{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
#pragma mark -
#pragma mark Compose Mail
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Ilusiones"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"anam@semanticnotion.com"];
[picker setToRecipients:toRecipients];
// Attach a screenshot to the email
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *myData = UIImagePNGRepresentation(viewImage);
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"viewImage"];
// Fill out the email body text
NSString *emailBody = @"";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
NSLog (@"entered the delegate"); // this line is not shown when the cancel button is tapped-meaning it never enters the delegate in that case.
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(@"Result: saved");
break;
case MFMailComposeResultSent:
NSLog( @"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog( @"Result: failed");
break;
default:
NSLog(@"Result: not sent");
break;
}
[self dismissModalViewControllerAnimated:YES]; //this works fine in the send case, and the email is sent. but hangs in the cancel case.
}
#pragma mark -
#pragma mark Workaround
-(void)launchMailAppOnDevice
{
NSString *recipients = @"mailto:anam@semanticnotion.com.com?cc=second@example.com,third@example.com&subject=illusions!";
NSString *body = @"&body=xyz";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}