Source: http://www.jianshu.com/p/c38a8a8edcf0
UIWebView loading a remote URL
NSURL *url = [NSURL URLWithString:@"[http://www.baidu.com]"];NSURLRequest *request = [NSURLRequest requestWithURL:url];[self.webView loadRequest:request];
-
UIWebView loading Local HTML
Method 1
//加载本地html NSString *basePath = [[NSBundle mainBundle] bundlePath]; NSString *htmlPath = [basePath stringByAppendingPathComponent:@"test.html"]; NSURL *url = [NSURL fileURLWithPath:htmlPath]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request];
Method 2
NSString *path = [[NSBundle mainbundle] pathforresource:@ "test" oftype:@ "html"]; Nsurl *url = [Nsurl Urlwithstring:path]; Self.webView.scalesPageToFit = YES; NSString *htmlstring = [NSString stringwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil]; [Self.webview loadhtmlstring:htmlstring Baseurl:url];
-
js call oc Method
There is an open-source framework on the web that enables native and JS to call each other directly Webviewjavascriptbridge, if just need simple call, can use UIWebView proxy method instead of -(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype , the code is as follows:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{NSString *url = [[request URL] absoluteString];// NSURL *url = [request URL];NSLog(@"%@",url);//代码中根据返回的URL或者scheme来判断处理不同逻辑if ([url isEqualToString:@"demo://"]){DetailViewController *detail = [[DetailViewController alloc] init];[self.navigationController pushViewController:detail animated:YES];}return YES;}
The HTML code is as follows:
<! DOCTYPE html>
Some usage of Ios-uiwebview (JS-oc method)