今天還有接下來的幾天要跟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-------");
};