如何在ios中解压后打开下载的文件

时间:2013-12-05 14:04:39

标签: ios objective-c

早上好。我使用以下示例在我的应用程序中下载并解压缩file

通常我的应用程序使用以下代码调用html文件:

- (IBAction)loadLocalFile:(id)sender
{
    UIButton *tmp = sender;
    int tag = tmp.tag;
    NSString *fileName = [NSString stringWithFormat:@"dossier%i", tag];
    NSURL *url = [NSURL URLWithString:@""];
    NSString *filePath = @"";

    switch (tag) {
        case 1:
        {
            filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"html"];
            break;
        }
        case 2:
        {
            filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"html"];
            break;
        }
        case 3:
        {
            filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"html"];
            break;
        }

        default:
            break;
    }

    url = [NSURL fileURLWithPath:filePath];
    LocalViewController *vc = [[LocalViewController alloc] initWithNibName:@"LocalViewController"     bundle:nil url:url];
    [self.navigationController pushViewController:vc animated:YES];
}

如何修改上面的代码,使其在documentsDir中查找,如果解压缩的文件在那里,请打开一个而不是应用程序包中的那个。

1 个答案:

答案 0 :(得分:1)

如果您想使用名称filePath的文件路径设置fileName,您可以使用以下内容:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName];