附加到电子邮件时,Zip文件不会到达

时间:2014-10-31 00:35:52

标签: ios cocoa-touch zip

我尝试从我的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

1 个答案:

答案 0 :(得分:0)

通常,电子邮件附件不会因以下两个原因之一而到达:

  1. 您用于加载NSData的路径导致数据为nil
  2. 该文件太大,无法通过电子邮件发送。
  3. 您有几个问题导致问题一。

    1. zip文件的路径位于只读文件夹中 - 应用程序资源包的根目录。
    2. 切勿使用NSHomeDirectory()创建应用沙盘中文件夹的路径。 iOS 8中的内容发生了变化,这些路径根本不起作用。仅将NSSearchPathForDirectoriesInDomainsNSDocumentDirectory
    3. 等适当的文件夹参考一起使用

      要解决第一个问题,请更改:

      NSString *archivePath = [documentsDirectory stringByAppendingString:@".zip"];
      

      类似于:

      NSString *archivePath = [documentsDirectory stringByAppendingPathComponent:@"SomeFiles.zip"];
      

      将此临时文件存储在缓存文件夹中会更好(使用NSCachesDirectory而不是NSDocumentDirectory)。然后在发送电子邮件后删除该文件。