0

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

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

在 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

4 回答 4

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];
于 2013-02-27T13:27:41.847 回答
1

下面的代码可能对您有用:

NSData *videoData = [NSData dataWithContentsOfURL:mediaUrl];

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

于 2013-03-04T11:47:39.063 回答
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 ,在这种情况下它将正常工作

于 2013-03-04T11:31:00.233 回答
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);

这是如何运作的?

于 2013-03-04T12:06:44.650 回答