Skip to content

Instantly share code, notes, and snippets.

@kwylez
Created November 10, 2011 16:45
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 kwylez/1355354 to your computer and use it in GitHub Desktop.
Save kwylez/1355354 to your computer and use it in GitHub Desktop.
GCD Loading of Local UIWebView
// loadRequest
UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
NSURL *htmlPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"about" ofType:@"html"] isDirectory:NO];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{
NSURLRequest *request = [NSURLRequest requestWithURL:htmlPath];
[webView loadRequest:request];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.view addSubview:webView];
[webView release];
});
});
// loadHTMLString
UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
NSURL *htmlPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"about" ofType:@"html"] isDirectory:NO];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{
NSURLRequest *request = [NSURLRequest requestWithURL:htmlPath];
NSString *htmlString = [NSString stringWithContentsOfURL:htmlPath
encoding:NSUTF8StringEncoding
error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.view addSubview:webView];
[webView release];
});
});
// loadHTMLData
UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
NSURL *htmlPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"about" ofType:@"html"] isDirectory:NO];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{
NSURLRequest *request = [NSURLRequest requestWithURL:htmlPath];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"about"
ofType:@"html"]];
[webView loadData:data MIMEType:@"text/html" textEncodingName:@"html" baseURL:nil];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.view addSubview:webView];
[webView release];
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment