我正在尝试通过电子邮件发送设备日志文件并使用以下代码。我能够通过Wi-Fi网络发送iPod Touch和iPad中的电子邮件。但是当我试图通过3G网络在iPhone上发送电子邮件时,就会崩溃。我推出了代码并且知道在执行presentModalViewController时崩溃了。
请帮助我,如何解决此问题。
- (IBAction)sendEmail:(id)sender
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// Set the subject of email
[picker setSubject:@"Debug Log"];
// Add email addresses
[picker setToRecipients:[NSArray arrayWithObjects:@"emailaddress1@domainName.com", @"emailaddress2@domainName.com", nil]];
// Fill out the email body text
NSString *emailBody = @"Debug Log content…… ";
// This is not an HTML formatted email
[picker setMessageBody:emailBody isHTML:NO];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory
stringByAppendingPathComponent:@"console.log"];
NSData *data = [NSData dataWithContentsOfFile:logPath];
[picker addAttachmentData:data mimeType:@"text/xml" fileName:@"console.log"];
// Show email view
[self presentModalViewController:picker animated:YES];//app crash
// Release picker
[picker release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Called once the email is sent
// Remove the email view controller
[self dismissModalViewControllerAnimated:YES];
}
答案 0 :(得分:3)
在发送邮件之前,请检查是否在iOS设备中配置了电子邮件。
您可以在发送邮件之前检查通过代码发送邮件的功能,如下所示。
- (IBAction)sendEmail:(id)sender
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// Set the subject of email
[picker setSubject:@"Debug Log"];
// Add email addresses
[picker setToRecipients:[NSArray arrayWithObjects:@"emailaddress1@domainName.com", @"emailaddress2@domainName.com", nil]];
// Fill out the email body text
NSString *emailBody = @"Debug Log content…… ";
// This is not an HTML formatted email
[picker setMessageBody:emailBody isHTML:NO];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory
stringByAppendingPathComponent:@"console.log"];
NSData *data = [NSData dataWithContentsOfFile:logPath];
[picker addAttachmentData:data mimeType:@"text/xml" fileName:@"console.log"];
// Show email view
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil) {
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail]) {
[self presentModalViewController:picker animated:YES];
} else {
//do something
}
} else {
//do something
}
// Release picker
[picker release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Called once the email is sent
// Remove the email view controller
[self dismissModalViewControllerAnimated:YES];
}
答案 1 :(得分:0)
简单的答案是使用此代码段将您的调用封装到presentModalViewController
:
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
if ([mailClass canSendMail])
{
[self presentModalViewController:YOURMAILCOMPOSER animated:YES];
}
}
这甚至可以为您的用户提供一个自动生成的警报视图,告诉他们在继续之前设置邮件帐户...