开发者

iPhone:Accessing documents directory file path in Safari

开发者 https://www.devze.com 2023-03-01 02:53 出处:网络
I have placed a file programmatically in documents folder on iPhone like below. NSFileManager *fileManager = [NSFileManager defaultManager];

I have placed a file programmatically in documents folder on iPhone like below.

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) {
    NSA开发者_Python百科ssert1(0, @”Failed to create writable database file with message ‘%@’.”, [error localizedDescription]);
}

I want to access this file in Safari by giving the path of local file through the below code.

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

Is it possible? I saw somewhere, we can use the URL starting with "file:///" . Is it true? If yes, how can i access my documents directory file path in Safari passing the URL via the above one line code?

Please advise.

Thank you!


I would expect that that is not possible because every app is sandboxed, which means your app's data is not accessible to any other app, and vice versa.

To add to that, file:// is not one of the recognized or supported iOS schemes, as far as I know.


See run locally created file on iPhone safari


If what you mean is to run the html in a browser inside the app that LOOKS LIKE Safari, you can use a UIWebView and call

 loadHTMLString:baseURL 

You can create your file URL like this:

 NSURL *myFileURL = [NSURL fileURLWithPath: pathToMyFile];

But like everyone else says, you can't access an app's file from any other app (without jailbreaking).

0

精彩评论

暂无评论...
验证码 换一张
取 消