本以为webview只是传个URL过去,让他自生自灭就好了,结果发现,还真不是这样,遇到过的使用方法总结一下,有新的再补充。
-
一、基础用法:初始化
声明全局
@interface WebViewController ()//代理
@property(nonatomic,strong)UIWebView * webview;
@endinit里
_webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height)];
[self.view addSubview:_webview];
_webview.delegate = self;//添加代理,否则不走代理方法
_webview.scrollView.showsVerticalScrollIndicator = NO;//不会根据navigationbar自适应高度
_webview.scalesPageToFit = YES;
[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://baidu.com"]]];
- 常用代理方法
-(void)webViewDidStartLoad:(UIWebView *)webView{
//开始加载时
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;//开启系统自带的webview小小菊花,在状态栏旋转的那个
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
//加载完成时
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;//关掉小花花
}
-(void)webView:(UIWebView )webView didFailLoadWithError:(NSError )error{
//加载失败
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
-(BOOL)webView:(UIWebView )webView shouldStartLoadWithRequest:(NSURLRequest )request navigationType:(UIWebViewNavigationType)navigationType{
//这里可以获取到加载的URL 等信息 并做一些处理
}
二、一些用法
1.获取cookie信息,可以获取一些后台传来的数据
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:_http]];
NSEnumerator *enumerator = [cookies objectEnumerator];
NSHTTPCookie *cookie;
while (cookie = [enumerator nextObject]) {
NSLog(@"cookie.name %@",cookie.name);
}
}
2.获取URL截取
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//在这里可以看到webview加载过程中的url
NSLog(@"request : %@ ",request.URL);
NSString *string = [NSString stringWithFormat:@"%@",request.URL];
if ([string rangeOfString:@"tologin"].location != NSNotFound ) {//判断所需的URL,做一些处理,这里是如果url里包含"tologin" 就跳转到login界面
BZPLoginViewController *loginVC = [[BZPLoginViewController alloc]init];
[self.navigationController pushViewController:loginVC animated:YES];
}
return YES;
}
3.与后台js交互,有时点击iOS端按钮等无响应时,需要后台哥们儿调用本地方法
//需要引入 #import <JavaScriptCore/JavaScriptCore.h>
-(void)webViewDidFinishLoad:(UIWebView *)webView{
JSContext *context = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"openWindow"] = ^() {//"openWindow"要和后台约定好
NSLog(@"[JSContext currentArguments] %@",[JSContext currentArguments]);
NSArray *arr = [JSContext currentArguments];
//然后对arr做处理就可以
for (id obj in [JSContext currentArguments]) {
NSString *str = [obj toString];//有时需要转换,否则会崩
if ([str isEqualToString:@"true"]) {
[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:HTTPURL_refreshPageUrl]]];
[_webview stopLoading];
[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:HTTPURL_refreshPageUrl]]];
}else{
[_webview goBack];
}
}
};
}
三、一些常用
1.获取当前webview的url
[_webview stringByEvaluatingJavaScriptFromString:@"document.location.href"];
(还有一些,后面再补充吧,要去吃饭咯)