将文件夹从服务器下载到本地iPad / iPhone

时间:2014-10-06 06:36:01

标签: ios iphone ipad download

我想创建一个应用程序,自动从我自己的服务器下载文件夹并将其本地存储在iPad / iPhone上。我怎样才能完成这个以及文件夹存储在本地iDevice上的哪个位置?因此,我将如何访问它?非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

最好的方法是将所有图片放在一个zip文件中,下载并使用以下代码在真实设备上解压缩:

dispatch_queue_t queue = dispatch_get_global_queue(
                                                   DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    NSURL *url = [NSURL URLWithString:@"someDirectory/newPics.zip"];
    NSError *error = nil;
    // 2
    NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];

    if(!error)
    {
        // 3
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *path = [paths objectAtIndex:0];
        NSString *zipPath = [path stringByAppendingPathComponent:@"newPics.zip"];

        [data writeToFile:zipPath options:0 error:&error];

        if(!error)
        {

            ZipArchive *za = [[ZipArchive alloc] init];
            // 1
            if ([za UnzipOpenFile: zipPath]) {
                // 2
                BOOL ret = [za UnzipFileTo: path overWrite: YES];
                if (NO == ret){} [za UnzipCloseFile];

                // 3
                NSString *imageFilePath = [path stringByAppendingPathComponent:@"newPics/pic1.png"];
                //[self removeImage:zipPath];


                [[NSFileManager defaultManager] removeItemAtPath:zipPath error: &error];

                dispatch_async(dispatch_get_main_queue(), ^{

                        NSURL *fileURL = [NSURL fileURLWithPath:imageFilePath];
                        [_webV loadRequest:[NSURLRequest requestWithURL:fileURL]];



                });

            }

        }
        else
        {
            NSLog(@"Error saving file %@",error);
        }
    }
    else
    {
        NSLog(@"Error downloading zip file: %@", error);
    }

});

这是最好的方法,快速可靠。