直接从应用程序附加mp3文件到电子邮件?

时间:2013-02-27 13:05:35

标签: iphone ios ipad

我正在开发一个应用程序,如果用户选择了声音,他们可以通过应用程序将其发送给自己。

附件部分“似乎”有效,但是,当我发送电子邮件时,收件人没有附件?

在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];

}

4 个答案:

答案 0 :(得分: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];

答案 1 :(得分:1)

以下代码可能对您有用:

NSData * videoData = [NSData dataWithContentsOfURL:mediaUrl];

[mailcomposer addAttachmentData:videoData mimeType:@“video / mp4”fileName:@“Video”]

答案 2 :(得分: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之类的有效扩展名,在这种情况下它将正常工作

答案 3 :(得分:0)

尝试使用mime类型的audio/mpeg代替。您可以通过运行以下代码来获取此值:

#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);

这是如何运作的?