我尝试从我的documents directory
压缩我的sqlite文件,使用https://github.com/mattconnolly/ZipArchive通过邮件发送。
NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"app.sqlite"];
NSString *filePath2 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"app.sqlite-shm"];
NSString *filePath3 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"app.sqlite-wal"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *archivePath = [documentsDirectory stringByAppendingString:@".zip"];
//Zip up files
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
[archiver addFileToZip:filePath newname:@"backUp.sqlite"];
[archiver addFileToZip:filePath2 newname:@"backUp.sqlite-shm"];
[archiver addFileToZip:filePath3 newname:@"backUp.sqlite-wal"];
MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
if (mailView != nil) {
mailView.mailComposeDelegate = self;
[mailView setSubject:@"Database"];
//Attach zip file
[mailView addAttachmentData:[NSData dataWithContentsOfFile:archivePath] mimeType:@"application/zip" fileName:@"test.zip"];
[mailView setMessageBody:@"Database attached" isHTML:NO];
[self presentViewController:mailView animated:YES completion: NULL];
}
附件会显示在设备上的电子邮件预览中,但当它与收件人一起到达时附件已消失了吗?
也尝试了这个:
NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"app.sqlite"];
NSString *filePath2 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"app.sqlite-shm"];
NSString *filePath3 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"app.sqlite-wal"];
DebugLog(@"DB path is: %@", filePath);
NSError *error = nil;
NSData *fileData = [[NSData alloc] initWithContentsOfFile:filePath options:0UL error:&error];
NSString* fileDataString = [[NSString alloc] initWithData:fileData encoding:NSNonLossyASCIIStringEncoding];
NSData *fileData2 = [[NSData alloc] initWithContentsOfFile:filePath2 options:0UL error:&error];
NSString* fileData2String = [[NSString alloc] initWithData:fileData2 encoding:NSNonLossyASCIIStringEncoding];
NSData *fileData3 = [[NSData alloc] initWithContentsOfFile:filePath3 options:0UL error:&error];
NSString* fileData3String = [[NSString alloc] initWithData:fileData3 encoding:NSNonLossyASCIIStringEncoding];
if (error != nil) {
DebugLog(@"Failed to read the file: %@", [error localizedDescription]);
} else {
if (fileData == nil) {
DebugLog(@"File data is nil");
}
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *archivePath = [documentsDirectory stringByAppendingString:@".zip"];
//Zip up documents directory
ZipArchive *archiver = [[ZipArchive alloc] init];
[archiver CreateZipFile2:archivePath];
[archiver addFileToZip:fileDataString newname:@"backUp.sqlite"];
[archiver addFileToZip:fileData2String newname:@"backUp.sqlite-shm"];
[archiver addFileToZip:fileData3String newname:@"backUp.sqlite-wal"];
MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
if (mailView != nil) {
mailView.mailComposeDelegate = self;
[mailView setSubject:@"Database"];
//Attach zip file
[mailView addAttachmentData:[NSData dataWithContentsOfFile:archivePath] mimeType:@"application/zip" fileName:@"test.zip"];
[mailView setMessageBody:@"Database attached" isHTML:NO];
[self presentViewController:mailView animated:YES completion: NULL];
}
相同的行为zip
文件未随电子邮件一起提交
记录archivePath
会给我/var/mobile/Containers/Data/Application/1D32A3AA-A431-46234-AE4B-ED944CA2D883F4/Documents.zip
答案 0 :(得分:0)
通常,电子邮件附件不会因以下两个原因之一而到达:
NSData
的路径导致数据为nil
。您有几个问题导致问题一。
NSHomeDirectory()
创建应用沙盘中文件夹的路径。 iOS 8中的内容发生了变化,这些路径根本不起作用。仅将NSSearchPathForDirectoriesInDomains
与NSDocumentDirectory
。要解决第一个问题,请更改:
NSString *archivePath = [documentsDirectory stringByAppendingString:@".zip"];
类似于:
NSString *archivePath = [documentsDirectory stringByAppendingPathComponent:@"SomeFiles.zip"];
将此临时文件存储在缓存文件夹中会更好(使用NSCachesDirectory
而不是NSDocumentDirectory
)。然后在发送电子邮件后删除该文件。