2013-02-27 79 views
0

我工作的一個應用程序,其中,如果用戶選擇一個聲音,它們可以從應用程序發送給自己。附加MP3文件直接從應用程序發送電子郵件?

安裝部「出現」工作,然而,當我發送電子郵件,收件人沒有附件?

在iPad/iPhone本身,它看起來是連接它時,它涉及到創作,但它不工作? :/

這裏是我使用的代碼;

- (void)onSend:(id)sender{ 

    int nIndex; 

    UIButton *btnSender = (UIButton *)sender; 
    NSLog(@"%d", btnSender.tag); 

    for (int i = 0; i < [ m_aryFileName count ]; i++) { 

     if(i == (btnSender.tag - 100)){ 
      nIndex = i; 
     } 

    } 

    NSString *strFileName = [ m_aryFileName objectAtIndex:nIndex ]; 
    strFileName = [ strFileName stringByAppendingString:@".mp3" ]; 
    NSData*  nData = [ NSData dataWithContentsOfFile:strFileName ]; 

    MFMailComposeViewController *pickerMail = [[MFMailComposeViewController alloc] init]; 
    pickerMail.mailComposeDelegate = self; 

    [pickerMail setSubject:@"myMail Attachment"]; 

    // Attach an image to the email 
    [pickerMail addAttachmentData:nData mimeType:@"audio/mp3" fileName:strFileName ]; 

    // Fill out the email body text 
    NSString *emailBody = @"Here is your attachment"; 

    [pickerMail setMessageBody:emailBody isHTML:YES]; 

    [self presentModalViewController:pickerMail animated:YES]; 
    [pickerMail release]; 

} 

回答

0

嘗試使用的audio/mpeg mime類型代替。您可以通過運行以下代碼獲取此值:

#import <MobileCoreServices/MobileCoreServices.h> 

CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, 
                 (__bridge CFStringRef)@"mp3", 
                 NULL); 
CFStringRef mimeTags = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType); 
CFRelease(uti); 
NSString *mediaType = [NSString stringWithString:(__bridge NSString *)mimeTags]; 
CFRelease(mimeTags); 

這是如何工作的?

1

試試這個代碼的隊友,

NSString *strFileName = [m_aryFileName objectAtIndex:nIndex]; 
strFileName = [strFileName stringByAppendingString:@".mp3"]; 
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:strFileName]; 
NSData *nData = [[NSData alloc] initWithContentsOfURL:fileURL]; 

MFMailComposeViewController *pickerMail = [[MFMailComposeViewController alloc] init]; 
pickerMail.mailComposeDelegate = self; 
[pickerMail setSubject:@"myMail Attachment"]; 
[pickerMail addAttachmentData:nData mimeType:@"audio/mpeg" fileName:strFileName ]; 
NSString *emailBody = @"Here is your attachment"; 
[pickerMail setMessageBody:emailBody isHTML:YES]; 
[self presentModalViewController:pickerMail animated:YES]; 
+0

它只是重視所謂ATT00001.txt :( – user1695971 2013-02-27 13:49:15

+0

你試試我的隊友代碼.txt文件? – 2013-02-27 13:51:06

+0

是的,它只是附加的文件爲.txt文件不是一個mp3文件,它不會打開。 – user1695971 2013-02-27 13:59:11

0
if ([MFMailComposeViewController canSendMail] && m_pDataArray != nil) 
{ 
    NSString * pSongPath = [[NSBundle mainBundle] pathForResource:@"song" ofType"@"mp3"]; ;//get the file 
    MFMailComposeViewController * pMailComposer = [[MFMailComposeViewController alloc] init]; 
    pMailComposer.mailComposeDelegate = self; 
    [pMailComposer setMessageBody:@"msg body" isHTML:NO]; 
    NSURL * pFileUrl = [[[NSURL alloc] initFileURLWithPath:pSongPath] autorelease]; 
    NSData * pData = [[[NSData alloc] initWithContentsOfURL:pFileUrl] autorelease]; 
    [pMailComposer addAttachmentData:pData mimeType:@"audio/mpeg" fileName:@"song.mp3" ]]; 
    [self presentModalViewController:pMailComposer animated:YES]; 
    [pMailComposer release]; 
} 
else 
{ 
    UIAlertView *pAlert = [[UIAlertView alloc] initWithTitle:@"Failure" message:@"Your device doesn't support the composer sheet"              delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [pAlert show]; 
    [pAlert release]; 
    pAlert = nil; 
} 

確保該文件應具有有效的擴展名像MP3播放,在這種情況下,它會正常工作

1

下面的代碼可能是對你有用:

NSData * videoData = [NSData dataWithContentsOfURL:mediaUrl];

[mailcomposer addAttachmentData:可視數據mime類型:@ 「視頻/ MP4」 文件名:@ 「視頻」]

+0

它是否適合你? – Mrunal 2013-03-04 14:05:06

+0

它的工作,所以其他代碼如果只有我可以給你這兩個正確的答案:( – user1695971 2013-03-04 23:28:53

+0

沒問題,很高興你的問題解決了。:) – Mrunal 2013-03-05 05:07:55

相關問題