【问题标题】:presentModalViewController crash while sending email on 3G在 3G 上发送电子邮件时,presentModalViewController 崩溃
【发布时间】:2012-07-21 19:16:38
【问题描述】:

我正在尝试通过电子邮件并使用以下代码发送设备日志文件。我可以在 iPod Touch 和 iPad 中通过 Wi-Fi 网络发送电子邮件。但是当我试图在 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];
}

【问题讨论】:

    标签: iphone email ios5 3g presentmodalviewcontroller


    【解决方案1】:

    在发送邮件之前,检查是否在 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];
    }
    

    【讨论】:

      【解决方案2】:

      简单的答案是使用以下代码 sn-p 封装您对 presentModalViewController 的调用:

      Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
      if (mailClass != nil)
      { 
          if ([mailClass canSendMail])
          {
              [self presentModalViewController:YOURMAILCOMPOSER animated:YES];
          }
      }
      

      这甚至会为您的用户提供一个自动生成的警报视图,告诉他们在继续之前设置邮件帐户...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-25
        • 1970-01-01
        • 2011-11-07
        相关资源
        最近更新 更多