【发布时间】:2010-11-25 08:11:26
【问题描述】:
我正在尝试将MFMailComposeViewController 合并到我的应用程序中。当我以模态方式呈现它时,发送按钮可以正常工作并发送电子邮件,这意味着在这种情况下发送给代表的结果是正确的。
而当我点击取消按钮时,它会挂断应用程序。日志也没有显示任何错误,只是屏幕变暗,一切都被禁用。显然,结果没有传递给委托(我通过日志检查了它)。看来
(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
永远不会在按下取消按钮时调用。可能这就是未显示操作表(保存草稿、取消、删除草稿)的原因,因此应用程序就挂在那里了。
我正在使用来自 Apple 示例应用程序 (MailComposer) 的确切代码,它在那里完美运行,但不知何故我的失败了。 :(
如果有人遇到过同样的问题并成功解决,请帮助我。
我的代码:
-(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
{
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];
}
#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]];
}
【问题讨论】:
标签: iphone objective-c ios4