活動模塊UIWebView

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

    };
發佈了61 篇原創文章 · 獲贊 6 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章