Search code examples
iphoneemailios53gpresentmodalviewcontroller

presentModalViewController crash while sending email on 3G


I am trying to send the device log file via email and using the below code. I am able to send the email in iPod Touch and iPad in wi-fi network. But when I am trying to send the email in iPhone on 3G network there is crash. I debuted the code and came to know that this is crashing while doing presentModalViewController.

Please help me ,how to resolve this issue.

- (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];
}

Solution

  • Before sending mail check that email is configured in the iOS device.

    You can check the capability for sending mail through code before sending mail as below.

    - (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];
    }