Skip to content

Instantly share code, notes, and snippets.

@ben-ng
Created November 14, 2014 20:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ben-ng/1efc627fe74aa66f2e93 to your computer and use it in GitHub Desktop.
Save ben-ng/1efc627fe74aa66f2e93 to your computer and use it in GitHub Desktop.
Use UIWebView's `loadHTMLString:baseURL:` and set the baseURL to where your assets are in the app bundle. Then replace all absolute paths in your html/js/css with relative ones using something like the script below. The relative URLs will be loaded relative to the baseURL, whereas absolute ones are loaded relative to the root of the filesystem, …
// This doodad converts absolute paths like "/assets/thing.jpg" into relative ones like "assets/thing.jpg"
NSString * (^fixPrefix)(NSString *, NSString *) = ^NSString *(NSString * input, NSString *prefix) {
NSString *needle = [[@"/" stringByAppendingString:prefix] stringByAppendingString:@"/"];
NSString *replacement = [prefix stringByAppendingString:@"/"];
input = [input stringByReplacingOccurrencesOfString:[@"'" stringByAppendingString:needle] withString:[@"'" stringByAppendingString:replacement]];
input = [input stringByReplacingOccurrencesOfString:[@"\"" stringByAppendingString:needle] withString:[@"\"" stringByAppendingString:replacement]];
input = [input stringByReplacingOccurrencesOfString:[@"(" stringByAppendingString:needle] withString:[@"(" stringByAppendingString:replacement]];
return input;
};
fileContents = fixPrefix(fileContents, @"assets";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment