1

以下のコードを使用して、デバイスのログファイルを電子メールで送信しようとしています。iPodTouchと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];
}
4

2 に答える 2

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];
}
于 2012-07-21T19:32:46.800 に答える
0

簡単な答えは、次のpresentModalViewControllerコード スニペットを使用してへの呼び出しをカプセル化することです。

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

これにより、続行する前にメールアカウントを設定するようにユーザーに指示するアラートビューが自動的に生成されます...

于 2014-05-25T09:09:22.757 に答える