1

I have downloaded a json file (data.json) using NSURLSession. I am trying to access this file from a local html file (main project folder) myfile.html which is displayed via UIWebView. From NSLog I have identified the temp file location is here:

file:///Users/administrator/Library/Developer/CoreSimulator/Devices/166B438D-4F67-448C-B0E5-B32438DA3BF9/data/Containers/Data/Application/FCA61482-BC5D-4804-91F8-891EAB4DB145/tmp/CFNetworkDownload_g6961Q.tmp

My question is how should I access this temp file from the local 'top level' html file, using a relative path?

More generally therefore - how does one access the iOS application file structure from such a top level (relative to the project) html file?

Some background info:

  1. When I manually copy data.json to XCode and access it from myfile.html using the relative path 'data.json' it works.

  2. The download code is:

    NSURL *URL = [NSURL URLWithString:@"http://myurl.com/data.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDownloadTask *downloadTask = [session     downloadTaskWithRequest:request completionHandler:
    ^(NSURL *location, NSURLResponse *response, NSError *error) {      
    }
    
  3. The reason I am doing this with NSURLSession is that I can't do it with Javascript in the html file since it is a cross domain request.

EDIT: Thanks for the helpful answers about adding HTML as a string into the UIWebView. I can see that trying to reference a temp file from the local HTML file is a bit unwieldly and may run counter to Apple's preference of not exposing filepaths - but I am still interested in a definitive answer on whether it can be done.

MrDave
  • 417
  • 1
  • 6
  • 18

2 Answers2

0

Perhaps you could inject the JSONData into the web page once it has finished loading within the web view. Something like this (I have not tested it):

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSError *error = nil;
    NSURL *URL = [NSURL URLWithString:@"http://myurl.com/data.json"];
    NSString *result = [NSString stringWithContentsOfURL:URL
                                                encoding:NSUTF8StringEncoding error:&error];
    if (!error && result.length > 0)
    {
        [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var jsonData = \"%@\"", result]];
    }
    else
    {
        NSLog(@"Oops: %@", error);
    }
}
Lachlan
  • 312
  • 2
  • 15
0

I don't think you can either. Instead of loading your resource HTML as-is, use it as a "template" and put a "placeholder" string where you need the link to your temp file.

Then, load your HTML into a (mutable) string, replace the placeholder for the actual path to the file and pass this modified HTML string to your web view.

Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189