Skip to content

Instantly share code, notes, and snippets.

@xdream86
Forked from amster/YourViewController.m
Created October 24, 2015 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdream86/dcab9c9e9234221ace88 to your computer and use it in GitHub Desktop.
Save xdream86/dcab9c9e9234221ace88 to your computer and use it in GitHub Desktop.
Load a UIWebView in iOS that can also load local resources like images, CSS, and JavaScript
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="your_styles.css" type="text/css">
<script src="your_javascript.js"></script>
</head>
<body>
<h1>Big Title</h1>
<!-- Again, all bundle resources must have unique filenames -->
<img src="some_image.jpg" width="100%">
<!-- Example interaction -->
<button id="exampleButton" onclick="document.getElementById('debug').innerHTML='Was clicked';">Click?</button>
<p id="debug"></p>
</body>
</html>
// An example viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadWebView];
}
// The example loader
//
// Assumes you have an IBOutlet for the UIWebView defined: @property (strong, nonatomic) UIWebView *wv;
- (void)loadWebView {
// Remember that bundle resources do *not* have directories so all filenames must be unique.
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *homeIndexUrl = [mainBundle URLForResource:@"home_index" withExtension:@"html"];
// The magic is loading a request, *not* using loadHTMLString:baseURL:
NSURLRequest *urlReq = [NSURLRequest requestWithURL:homeIndexUrl];
[self.wv loadRequest:urlReq];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment