当我点击我的邮件按钮时,我正在我的应用程序中实现MFMailComposeViewController我得到以下错误。
应用程序尝试在目标强文本
上显示nil模态视图控制器这是我的代码:
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
mailComposer = [[MFMailComposeViewController alloc]init];
mailComposer.navigationBar.tintColor = [UIColor blackColor];
[mailComposer.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor blackColor]}];
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:@"Co - Contact Us"];
[mailComposer setToRecipients:@[@"contact@co.org"]];
NSString *supportText = @"Enter your comment here:\n\n\n\n\n";
supportText = [supportText stringByAppendingString:[NSString stringWithFormat:@"iOS Version: %@\nCorner Version:%@\nBuild:%@\n\n",iOSVersion,version, build]];
[mailComposer setMessageBody:supportText isHTML:NO];
[self presentViewController:mailComposer animated:YES completion:nil];
我的代码出了什么问题。请帮忙
答案 0 :(得分:3)
如果设备未配置邮件或使用模拟器可能导致崩溃或异常。
mailComposer = [[MFMailComposeViewController alloc] init];
上面的代码行可能会导致问题。在模拟器初始化方法中,可能会返回NULL
或nil
。
所以,在呈现modal viewController:
之前,只需检查canSendMail
像,
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString * build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
mailComposer = [[MFMailComposeViewController alloc]init];
if ([MFMailComposeViewController canSendMail] && mailComposer) {
mailComposer.navigationBar.tintColor = [UIColor blackColor];
[mailComposer.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:@"Corner - Contact Us"];
[mailComposer setToRecipients:@[@"contact@corner.org"]];
NSString *supportText = @"Enter your comment here:\n\n\n\n\n";
supportText = [supportText stringByAppendingString:[NSString stringWithFormat:@"iOS Version: %@\nCorner Version:%@\nBuild:%@\n\n",iOSVersion,version, build]];
[mailComposer setMessageBody:supportText isHTML:NO];
[self presentViewController:mailComposer animated:YES completion:nil];
}