2017-02-24 61 views
-1

我的应用程序正在播放的铃声从URL以“.m4r”扩展名,我把他们叫做itemArray这样一个数组:如何安装在线音频文件的目标C到电子邮件

- (void)viewDidLoad{ 
    [super viewDidLoad]; 

    self.title = @"Ringtones"; 

    itemArray = [[NSArray arrayWithObjects: 
       [NSDictionary dictionaryWithObjectsAndKeys:@"Song Title1", @"song", @"Unknown", @"artise", @"http://www.mywebsite.com/", @"url", @"song.m4r", @"songId", nil], 
       [NSDictionary dictionaryWithObjectsAndKeys:@"Song Title2", @"song", @"Unknown", @"artise", @"http://www.mywebsite.com/", @"url", @"song.m4r", @"songId", nil], 
       [NSDictionary dictionaryWithObjectsAndKeys:@"Song Title3", @"song", @"Unknown", @"artise", @"http://www.mywebsite.com/", @"url", @"song.m4r", @"songId", nil], 
       [NSDictionary dictionaryWithObjectsAndKeys:@"Song Title4", @"song", @"Unknown", @"artise", @"http://www.mywebsite.com/", @"url", @"song.m4r", @"songId", nil], nil] retain]; 
} 

现在我需要什么做的是给用户带来附加这些文件以电子邮件的下载按钮,当轻敲选项,如下图所示:

- (void)downloadAudio:(DownloadButton *)dwnButton{ 
    NSInteger index = dwnButton.tag; 
    NSString *selectedFile = [itemArray objectAtIndex:index]; 
    [self showEmail:selectedFile]; 
} 

- (void)showEmail:(NSString *)file{ 

    if ([MFMailComposeViewController canSendMail]){ 

     NSString *emailTitle = @"Ringtones"; 
     NSString *messageBody = @"Enjoy our Awesome ringtones!"; 
     NSArray *toRecipents = [NSArray arrayWithObject:@""]; 

     MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
     mc.mailComposeDelegate = self; 
     [mc setSubject:emailTitle]; 
     [mc setMessageBody:messageBody isHTML:NO]; 
     [mc setToRecipients:toRecipents]; 

     NSString *urlString = [NSString stringWithFormat:@"%@/%@", _audioPlayer.url, _audioPlayer.songId]; 
     NSArray *filepart = [urlString componentsSeparatedByString:@"/"]; 
     NSString *filename = [filepart lastObject]; 


     // Get the resource path and read the file using NSData 
     NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:@"m4r"]; 
     NSData *fileData = [NSData dataWithContentsOfFile:filePath]; 

     // Add attachment 
     [mc addAttachmentData:fileData mimeType:@"audio/mpeg-4" fileName:filename]; 

     // Present mail view controller on screen 
     [self presentViewController:mc animated:YES completion:nil]; 

     [mc release]; 
    } 
    else{ 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" 
               message:@"Your device doesn't support the composer sheet" 
               delegate:nil 
             cancelButtonTitle:@"OK" 
             otherButtonTitles: nil]; 
     [alert show]; 
     [alert release]; 
    } 
} 

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{ 

    switch (result){ 

     case MFMailComposeResultCancelled: 
      NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued."); 
     break; 

     case MFMailComposeResultSaved: 
      NSLog(@"Mail saved: you saved the email message in the drafts folder."); 
     break; 

     case MFMailComposeResultSent: 
      NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send."); 
     break; 

     case MFMailComposeResultFailed: 
      NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error."); 
     break; 

     default: 
      NSLog(@"Mail not sent."); 
     break; 
    } 

    // Remove the mail view 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

我的问题是我每次下载按钮的应用程序以“终止应用程序由于崩溃挖掘时间未捕获的异常NSInternalInconsistencyException,原因:-[MFMailComposeInternalViewController addAttachmentData:mimeType:fileName:] attachment must not be nil.

***第一掷调用堆栈: (0x1938091b8 0x19224055c 0x19380908c 0x1942c102c 0x19ffc64b0 0x1000605f8 0x10006033c 0x1996f3d30 0x1996f3cb0 0x1996de128 0x1996f359c 0x199c7e628 0x199c7a6c0 0x199c7a1e0 0x199c7949c 0x1996ee30c 0x1996beda0 0x199ea875c 0x199ea2130 0x1937b6b5c 0x1937b64a4 0x1937b40a4 0x1936e22b8 0x195196198 0x1997297fc 0x199724534 0x1000bbc2c 0x1926c55b8) 的libC++ ABI。 dylib:以NSException类型的未捕获异常终止“错误。

任何帮助将不胜感激[BTW应用程序是我真正的设备而不是模拟器上运行。

回答

0

[MFMailComposeInternalViewController addAttachmentData:mime类型:文件名:]附件不能是零

fileData要发送作为电子邮件附件是nil当调用[mc addAttachmentData:fileData mimeType:@"audio/mpeg-4" fileName:filename]; 。这是事故的根源。

不建议通过dataWithContentsOfFile:获取远程内容。

您必须考虑使用NSURLSession API。
This & this对这个话题有很好的解读。

另一种实现可以为:

- (void)showEmail:(NSString *)file{ 

    if([MFMailComposeViewController canSendMail]){ 

     NSURLSession *sessionWithoutDelegate = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
     NSString *urlString = [NSString stringWithFormat:@"%@/%@", _audioPlayer.url, _audioPlayer.songId]; 
     NSURL *url = [NSURL URLWithString:urlString]; 

     [[sessionWithoutDelegate dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 

      NSString *emailTitle = @"Ringtones"; 
      NSString *messageBody = @"Enjoy our Awesome ringtones!"; 
      NSArray *toRecipents = [NSArray arrayWithObject:@""]; 

      MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
      mc.mailComposeDelegate = self; 
      [mc setSubject:emailTitle]; 
      [mc setMessageBody:messageBody isHTML:NO]; 
      [mc setToRecipients:toRecipents]; 

      NSString *urlString = [NSString stringWithFormat:@"%@/%@", _audioPlayer.url, _audioPlayer.songId]; 

      NSArray *filepart = [urlString componentsSeparatedByString:@"/"]; 
      NSString *filename = filepart.lastObject; 

      // use the downloaded data here 
      [mc addAttachmentData:data mimeType:@"audio/mpeg-4" fileName:filename]; 

      [self presentViewController:mc animated:YES completion:nil]; 

     }] resume]; 
    } 
    else{ 
     //... 
    } 
} 
+0

@ ystack感谢您的回答,其实你的答案正在与一些条件,我的意思是我必须按每个文件是播放按钮能够点击下载按钮,但是当我在应用程序启动应用程序崩溃后点击下载按钮。我不知道到底是什么问题。你有什么主意吗?我需要先保存文件然后附加它吗?如果是这样的话,我该如何解决这个问题? – SwiftyLearner

相关问题