今天还有接下来的几天要跟webView打交道了:
下边是我找的一些跟WebvView相关的资料:
http://www.jianshu.com/p/3d7e4804033b
http://bxbxbai.gitcafe.io/2015/08/16/talk-about-bybird-app/
http://www.jianshu.com/p/ca496cb680fe
首先,让我们了解一下UIWebView。
加载本地html页面:
NSString *webPath = [[NSBundle mainBundle]pathForResource:@“HelloWord"ofType:@"html”];//获取文件路径
NSURL *webURL = [NSURL fileURLWithPath:webPath];//通过文件路径字符串设置URL
NSURLRequest *URLRequest = [NSURL RequestrequestWithURL:webURL];//设置请求提交的相关URL
[self.webViewloadRequest:URLRequest];//提交请求
1、请求网络获取html
我们的项目需要带参数的网络请求,返回一个html页面
//********************** AF begin *************************
//增加这几行代码;
AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
[securityPolicy setAllowInvalidCertificates:YES];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//这里进行设置;
[manager setSecurityPolicy:securityPolicy];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:ACTIVITYLIST
parameters:params
success:^(AFHTTPRequestOperation *operation,id responseObject){
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"成功: %@", string);
}
failure:^(AFHTTPRequestOperation *operation,NSError *error){
NSLog(@"失败: %@", error);
}];
//********************** AF over ********************
2、拿到str之后展示在webView上
[self.activityWebView loadHTMLString:string baseURL:nil];
3、拦截URL处理
在代理方法-(BOOL)webView:(UIWebView
)webView shouldStartLoadWithRequest:(NSURLRequest )request navigationType:(UIWebViewNavigationType)navigationType中拦截处理跳转URL
4、js调用Object-c函数
//js交互
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//未登录或者token失效,请求登录,toLoginByAPP是约定好的函数名称
context[@"toLoginByAPP"] = ^() {
NSLog(@"+++++++Begin toLoginByAPP+++++++");
NSArray *args = [JSContext currentArguments];
for (JSValue *jsVal in args) {
NSLog(@"toLoginByAPP:%@", jsVal);
}
JSValue *this = [JSContext currentThis];
NSLog(@"toLoginByAPP——this: %@",this);
NSLog(@"-------End toLoginByAPP-------");
};