iPhone:访问Safari中的文档目录文件路径

时间:2011-04-25 19:36:12

标签: iphone

我已经以编程方式将文件放在iPhone上的文档文件夹中,如下所示。

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSError *error;
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"myfilename.xxxxx"];


NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myfilename.xxxxx"];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
    NSAssert1(0, @”Failed to create writable database file with message ‘%@’.”, [error localizedDescription]);
}

我希望通过以下代码提供本地文件的路径来访问Safari中的此文件。

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:localFileURL] ];

有可能吗?我在某个地方看到,我们可以使用以“file:///”开头的URL。这是真的吗?如果是,我如何通过上面的一行代码访问Safari中的文档目录文件路径?

请告知。

谢谢!

3 个答案:

答案 0 :(得分:1)

我希望这是不可能的,因为每个应用都是沙盒,这意味着您的应用的数据无法被任何其他应用访问,反之亦然。

除此之外,据我所知,file://不是公认的或支持的iOS方案之一。

答案 1 :(得分:0)

答案 2 :(得分:0)

如果您的意思是在LOOKS LIKE Safari的应用程序内的浏览器中运行html,您可以使用UIWebView并调用

 loadHTMLString:baseURL 

您可以像这样创建文件网址:

 NSURL *myFileURL = [NSURL fileURLWithPath: pathToMyFile];

但与其他人说的一样,你无法从任何其他应用程序访问应用程序的文件(没有越狱)。

相关问题