I am working with a Drupal developer to develope an iPhone app that fetches a portion of it's content from a Drupal site, stores it in a file system, and displays it locally in a UIWebView. We've run into a bit of a problem though, and we're looking for a solution, either in Drupal or on the iPhone end of things.
The Setup
- As Drupal content changes, it will create manifests of the changed content that the iPhone can download. The manifests simply list relative links to the new content to download.
- Once downloaded, the app stores the content locally in a web page.
- The iPhone loads the content into a UIWebView; links navigate between all the various pages.
The Problem
- Drupals relative links are relative to the root of the site - i.e. they contain a leading '/'. Obviously when I'm loading pages, links relative to the root of the filesystem won't work, since the content resides in the app's document directory. I can prepend the document directory to links the user taps using
-webView:shouldStartLoadWithRequest:navigationType:
, checking to see if the NSURLRequest contains a url starting with the document directory and redirecting it if it does not. However, this does not help with page resources - e.g. CSS files and images. - Drupal does not append file extensions to paths, which creates a similar dilemma. UIWebView's
-loadRequest
method only seems to work if it is fed an NSURLRequest that contains a URL to a file with a file extension. Otherwise I receive a WebKitErrorDomain error code 102: "Frame load interrupted". It may be possible to work out a solution using UIWebView's-loadHTMLString:baseURL:
, but that method is very unfriendly to the code when the user taps on links (I'm not sure why at this point).
Disregarding -release
messages, the code I am using to load pages is:
NSString *fileToLoad = [appDelegate.webContentDirectory stringByAppendingPathComponent:filename];
NSURL *url = [[NSURL alloc] initFileURLWithPath:fileToLoad isDirectory:NO];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:(NSTimeInterval)5];
[self.webView loadRequest:newURLRequest];
I am open to creative thinking - e.g. is there an iPhone web server I could set up in-app that would automatically take care of both problems?