在使用的过程中,需要遵守WebViewDelegate的协议方法
1:webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//这一句话是进行合适的加载引用的方法;
NSString *string=[NSString stringWithFormat:@"http://10.99.99.206:81/announcements/%@/app_show",self.string1];
// NSString *string=@"http://10.99.99.206:81/announcements/5653f98daf4843404c000008/app_show";//网页的请求地址
self.view.backgroundColor=[UIColor whiteColor];
webView.delegate=self;
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:string]];
NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=self.view.frame.size.width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\""];
[webView stringByEvaluatingJavaScriptFromString:meta];
//(initial-scale是初始缩放比,minimum-scale=1.0最小缩放比,maximum-scale=5.0最大缩放比,user-scalable=yes是否支持缩放)
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '40%'"];//修改百分比即可
[webView loadRequest:request];
[self.view addSubview:webView];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
// [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '40%'"];//修改百分比即可
// NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=self.view.frame.size.width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=3.0, user-scalable=yes\""];
// [webView stringByEvaluatingJavaScriptFromString:meta];
//(initial-scale是初始缩放比,minimum-scale=1.0最小缩放比,maximum-scale=5.0最大缩放比,user-scalable=yes是否支持缩放)
}
2:这个是
-(void)viewWillAppear:(BOOL)animated
{
// self.navigationController.navigationBar.backIndicatorImage=[UIImage imageNamed:@"tabbar"];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tabbar"] forBarMetrics:UIBarMetricsDefault];
//需要修改的时候修改颜色的方法;到时间换了图片就好;
}
-(void)viewWillDisappear:(BOOL)animated
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"naviBar"] forBarMetrics:UIBarMetricsDefault];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
5:webView的bug
Bug现象:webView加载指定url的期间,会被动收到很多加载请求,但请求的地址大多为中国移动广告、空地址“about:blank”或者 “http://221.179.140.145:30000/tlbagui/traffic/html/all.html?page=overview&tid=4&cid=2&time=1452569287421” “ http://17jifen.bj.chinamobile.com/hd/toolbar/index.html” “ http://frp.orientalwisdom.com/udp/html/crayon.html” “ http://221.179.140.145:30000/tlbagui/research/html/about.html?v1.0”
解决方法:为了避免加载无用链接,需要在webView的代理方法( shouldStartLoadWithRequest)中对请求进行过滤,非所需请求不进行加载,直接 return NO 即可。