Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After some more more work I found the solution to my problem. And answer here with what I found.</p> <p>The solution is to to create a custom NSURLProtocol implementation and register this in the application as handler for the http protocol. The code looks like this (I've omitted some error handling in the code):</p> <pre><code>#import "MyHttpURLProtocol.h" @implementation MyHttpURLProtocol + (BOOL)canInitWithRequest:(NSURLRequest *)request { return [[[request URL] scheme] isEqualToString:@"http"]; // we handle http requests } + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { return request; } - (void) startLoading { id&lt;NSURLProtocolClient&gt; client = [self client]; NSURLRequest* request = [self request]; NSString *pathString = [[request URL] absoluteString]; NSString* fileToLoad = nil; if ([pathString isEqualToString:@"http://example.com/index.html"]) { fileToLoad = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; } else if ([pathString hasPrefix:@"http://example.com/code.js"]) { fileToLoad = [[NSBundle mainBundle] pathForResource:@"code" ofType:@"js"]; } else if ([pathString isEqualToString:@"http://example.com/jquery-1.6.2.min.js"]) { fileToLoad = [[NSBundle mainBundle] pathForResource:@"jquery-1.6.2.min" ofType:@"js"]; } NSData* data = [NSData dataWithContentsOfFile:fileToLoad]; NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:[NSDictionary dictionary]]; [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; [client URLProtocol:self didLoadData:data]; [client URLProtocolDidFinishLoading:self]; } - (void) stopLoading {} @end </code></pre> <p>This code allows now to load the index.html file be accessing the following code:</p> <pre><code>[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/index.html"]]]; </code></pre> <p>And don't forget to register the custom protocol in your app delegate:</p> <p><del>[NSURLProtocol registerClass:[TOHttpURLProtocol class]];</del></p> <pre><code>[NSURLProtocol registerClass:[MyHttpURLProtocol class]]; </code></pre> <p>It's amazing how simple the solution can be after your found it. Here is the link to the updated example project: <a href="http://dl.dropbox.com/u/5426092/LocalCacheJsInjectTest-working.zip">http://dl.dropbox.com/u/5426092/LocalCacheJsInjectTest-working.zip</a></p> <p>And for completeness sake here is the link to the blog post which helped me in finding the solution: <a href="http://www.infinite-loop.dk/blog/2011/09/using-nsurlprotocol-for-injecting-test-data/">http://www.infinite-loop.dk/blog/2011/09/using-nsurlprotocol-for-injecting-test-data/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload