WebView
self.webview.dataDetectorTypes = UIDataDetectorTypeAll;
self.webview.scalesPageToFit = YES;
- 加载本地
html
文件,mp4
文件,doc
文件,pdf
文件
NSURL *url = [[NSBundle mainBundle]URLForResource:@"index.html" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
- 加载
string
数据,可以加载网站URL
的数据返回data
,解析data
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self.webView loadHTMLString:html baseURL:nil];
OC调用JS的代码
self.webView.delegate = self;
代理方法
加载完webview
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[webView stringByEvaluatingJavaScriptFromString:@"alert('hello world!')"];
}
JS调用OC的代码
代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if([request.URL.scheme isEqualToString:@"source"]){
NSString *methodName = request.URL.pathComponents[1];
NSString *param = request.URL.pathComponents[2];
SEL medthod = NSSelectorFromString(methodName);
if([self performSelector:medthod]){
[self performSelector:medthod withObject:param];
}
return NO;
}
return YES;
}