iOSUIWebView與js的交互

最近在iOS項(xiàng)目中需要使用到oc與js之間的相互調(diào)用,而且要求是實(shí)現(xiàn)方式必須與Android中的相同,方便js中統(tǒng)一處理。于是在對(duì)第三方庫(kù)WebViewJavascriptBridge進(jìn)行研究之后,仿照Android中的WebView與JS的交互機(jī)制,實(shí)現(xiàn)了一個(gè),在這里分享給大家。

首先要說(shuō)明的是,在iOS中js調(diào)用Objective-C的代碼只能通過(guò)重定向的形式進(jìn)行,即js中通過(guò)修改iframe的src,或者直接跳轉(zhuǎn)到一個(gè)url,在Objective-C中通過(guò)UIWebView的

webView:shouldStartLoadWithRequest:navigationType:方法攔截這個(gè)跳轉(zhuǎn),然后通過(guò)解析跳轉(zhuǎn)的url獲取js需要調(diào)用的方法名和參數(shù)。而在Android中,只需要調(diào)用WebView的addJavascriptInterface方法,將一個(gè)js對(duì)象綁定到一個(gè)java類(lèi),在類(lèi)中實(shí)現(xiàn)相應(yīng)的函數(shù),當(dāng)js需要調(diào)用java的方法時(shí),只需要直接在js中通過(guò)綁定的對(duì)象調(diào)用相應(yīng)的函數(shù)即可。

顯然Android中js交互的方式要比iOS上方便得多,因此,我們可以在iOS上實(shí)現(xiàn)一套與Android相類(lèi)似的機(jī)制。下面先說(shuō)明一下實(shí)現(xiàn)的原理,要在js中直接通過(guò)綁定的對(duì)象調(diào)用相應(yīng)的函數(shù),那么就需要在js中添加相應(yīng)的代碼,但是為了確保與Android的一致性,js代碼應(yīng)該在客戶端以注入的形式加入。所以,我們先實(shí)現(xiàn)一下需要注入的代碼:
詳見(jiàn):DEMO
DEMO2
javaScript:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {  
      if (webView != _webView) { return; }  
      //is js insert  
      //這句代碼的原理就是如果html文件中不包含在app中定義的方法就在這里獲取然后然會(huì)給js
      if (![[webView stringByEvaluatingJavaScriptFromString:  [NSString stringWithFormat:@"typeof window.%@ == 'object'", kBridgeN  ame]] isEqualToString:@"true"]) {  
          //get class method dynamically  
          unsigned int methodCount = 0;  
          Method *methods = class_copyMethodList([self class], &methodCount);  
          NSMutableString *methodList = [NSMutableString string];  
          for (int i=0; i<methodCount; i++) {  
              NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding];  
              [methodList appendString:@"\""];  
              [methodList appendString:  [methodName stringByReplacingOccurrencesOfString:@":" withString:@""  ]];  
              [methodList appendString:@"\","];  
          }  
          if (methodList.length>0) {  
              [methodList deleteCharactersInRange:NSMakeRange(methodList.length-1, 1)];  
          }  
          
          NSBundle *bundle = _resourceBundle ? _resourceBundle : [NSBundle mainBundle];  
          NSString *filePath = [bundle pathForResource:@"WebViewJsBridge" ofType:@"js"];  
          NSString *js = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];  
          [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:js, methodList]];  
      }  
  
      __strong typeof(_webViewDelegate) strongDelegate = _webViewDelegate;  
      if (strongDelegate && [strongDelegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {  
          [strongDelegate webViewDidFinishLoad:webView];  
      }  
    }  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容