我正在尝试从 iOS 应用程序附加一个 wav 文件,但即使附件在撰写的邮件中可见,也未送达。
相关代码如下:
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:NSLocalizedString(@"mailTopic", nil)];
[controller setMessageBody:NSLocalizedString(@"mailBody", nil) isHTML:YES];
NSString *wavPath = [self exportAssetAsWaveFormat:self.myRec.soundFilePath]; // CAF->Wav export
if (wavPath != nil) {
NSLog(@"wavPath: %@", wavPath);
NSData *recData = [NSData dataWithContentsOfFile:wavPath];
NSString *mime = [self giveMimeForPath:wavPath];
[controller addAttachmentData:recData mimeType:mime fileName:@"MySound.wav"];
[self presentModalViewController:controller animated:YES];
[controller release];
}
}
-(NSString *) giveMimeForPath:(NSString *)filePath {
NSURL* fileUrl = [NSURL fileURLWithPath:filePath];
NSURLRequest* fileUrlRequest = [[NSURLRequest alloc] initWithURL:fileUrl cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:.1];
NSURLResponse* response = nil;
[NSURLConnection sendSynchronousRequest:fileUrlRequest returningResponse:&response error:nil];
NSString* mimeType = [response MIMEType];
NSLog(@"MIME: %@", mimeType);
[fileUrlRequest release];
return mimeType;
}
NSLog 结果:
NSLog(@"wavPath: %@", wavPath); ->“wavPath:/var/mobile/Applications/71256DCA-9007-4697-957E-AEAE827FD97F/Documents/MySound.wav”
NSLog(@"MIME: %@", mimeType); ->“MIME:音频/wav”
文件路径接缝没问题(参见 NSLog 数据),并且 mime 类型设置为“audio/wav”..无法弄清楚这一点..
最佳答案
错误是当我用它创建 NSData 时,wav 文件还没有 100% 写入..duuuh
感谢大家的努力
关于cocoa-touch - Objective C - 通过 MFMailComposeViewController 的附件未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10381392/