我已经设置了一个MFMailComposeViewController,它可以在iPhone上正常工作,但在iPad上崩溃,说:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target...
那为什么会创建一个零模态视图呢?
MFMailComposeViewController *message = [[MFMailComposeViewController alloc] init];
[message setMessageBody:@"My message here" isHTML:NO];
[message setToRecipients:[NSArray arrayWithObject:@"my@domain.com"]];
[message setSubject:@"Request Info"];
message.mailComposeDelegate = self;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
message.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:message animated:YES];
[message release];
有什么想法吗?
答案 0 :(得分:73)
MFMailComposeViewController
之类的Loos由于某种原因未创建,因此具有nil
值。在呈现之前检查它是否为零(虽然这种解决方法不能回答这里出了什么问题......)。
您还应该检查邮件编辑器是否可以在尝试使用+canSendMail
方法创建和发送邮件之前发送邮件(例如,如果设备上没有设置邮件帐户,则会返回NO):
if ([MFMailComposeViewController canSendMail]){
// Create and show composer
}
else{
// Show some error message here
}
答案 1 :(得分:11)
在创建canSendMail
对象之前,您必须放置检查MFMailComposerViewController
,请参阅MFMailComposeViewController.h
类的以下注释:
/*!
@method canSendMail
@abstract Returns <tt>YES</tt> if the user has set up the device for sending email.
@discussion The client may continue to set the recipients and content if the return value was <tt>YES</tt>. If <tt>NO</tt>
was the result, the client has a couple options. It may choose to simply notify the user of the inability to
send mail, or it may issue a "mailto" URL via <tt>-[UIApplication openURL:]</tt>.
*/
+ (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
在您的设备设置为发送邮件之前,您的对象不会被初始化。
答案 2 :(得分:4)
if ([MFMailComposeViewController canSendMail]){
//execute your code
else{
UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[anAlert addButtonWithTitle:@"OK"];
[anAlert show];
}
如果您收到提醒,请在手机上配置您的邮件帐户。
答案 3 :(得分:2)
这是因为您的iOS默认邮件应用程序尚未配置任何邮件ID。所以使用你的任何邮件ID进行配置并尝试。
像这样if ([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setToRecipients:[NSArray arrayWithObject:eMail]];
[self presentViewController:controller animated:YES completion:nil];
}
else{
UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[anAlert addButtonWithTitle:@"Cancel"];
[anAlert show];
}
希望对你有所帮助。
答案 4 :(得分:-3)
您的测试设备上没有设置邮件帐户。
if ([MFMailComposeViewController canSendMail]){
//execute your code else{
UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"error" message:@"No mail account setup on device" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[anAlert addButtonWithTitle:@"Cancel"];
[anAlert show];
}