创建一个webView :UIWebView*_myWebView=[[UIWebViewalloc]initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height-48)];
_myWebView.delegate=self;
[self.viewaddSubview:_myWebView];
1. 加载webView不带有绿字横线,取消打电话的功能。 _myWebView.dataDetectorTypes=UIDataDetectorTypeNone;
2.webView 不弹动 _myWebView.scrollView.bounces=NO;
3.加载一个URL
NSURL* url = [NSURLURLWithString:@"https://www.baidu.com"];//创建URL
NSURLRequest* request = [NSURLRequestrequestWithURL:url];//创建NSURLRequest
[_myWebViewloadRequest:request];//加载
4.加载一个本地的html
(1).NSString*path = [[NSBundlemainBundle]pathForResource:@"about_12xue_frm"ofType:@"html"];
NSString*htmlString = [NSStringstringWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:nil];
NSString*basePath = [[NSBundlemainBundle]bundlePath];
NSURL*baseURL = [NSURLfileURLWithPath:basePath];
[_myWebViewloadHTMLString:htmlStringbaseURL:baseURL]; 这种是拖入html和图片时选择第一种Create groups。 路径无所谓
(2)Create folder references
这个需要把html放在文件夹里 路径完全需要按照写html的格式,比如拖进去了一个名叫htmlD的文件夹,htmlD里边的关系按相对路径算。
NSURL*url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"about_12xue_frm"ofType:@"html"inDirectory:@"htmlD"]];
NSURLRequest*request = [NSURLRequest requestWithURL:url];
[_myWebViewloadRequest:request];
5.加载一个的字符串
[_myWebViewloadHTMLString:@"123456789"baseURL:nil];
可配上加载后的代理
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView*)webView
{
NSString*meta = [NSStringstringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"",kDeviceWidth];
[webViewstringByEvaluatingJavaScriptFromString:meta];
}
网友评论