我有一个应用程序,它使用以下代码创建带附件的应用程序内邮件:
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
[controller setMailComposeDelegate:self];
[controller setToRecipients:recipient != nil ? [NSArray arrayWithObject:recipient] : nil];
[controller setSubject:subject];
[controller setMessageBody:body isHTML:isHTML];
[controller addAttachmentData:attachmentData mimeType:attachmentType fileName:attachmentFileName];
其中attachmentData是通过
创建的NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// build the file name using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/%@", documentsDirectory, attachmentFileName];
NSData *attachmentData = [NSData dataWithContentsOfFile:fileName];
通过
推断出mime类型 /*
* Infer the mime type
*/
NSString* attachmentMimeType = nil;
@try {
NSString* fullPath = [fileName stringByExpandingTildeInPath];
NSURL* fileUrl = [NSURL fileURLWithPath:fullPath];
NSURLRequest* fileUrlRequest = [NSURLRequest requestWithURL:fileUrl];
NSError* error = nil;
NSURLResponse* response = nil;
[NSURLConnection sendSynchronousRequest:fileUrlRequest returningResponse:&response error:&error];
attachmentMimeType = [response MIMEType];
}
@catch (NSException * e) {
// Test
}
@finally {
}
/*
* Default mime type is application/octet-stream
*/
if(attachmentMimeType == nil) attachmentMimeType = @"application/octet-stream";
代码在iOS 4,iOS 5,iOS 6下按预期工作。但是,在iOS 7.0下,附件缺失。这是iOS错误还是上述代码的问题?