IOS JS 호출 OC

2629 단어 objectivec
JS 호출 OC
이제 WKScriptMessage Handler 를 사용 해 야 돼 요.

 //  .m     
 @property (nonatomic ,strong)WKUserContentController * userCC;


//1   WKScriptMessageHandler  
//2    
   WKWebViewConfiguration * config = [[WKWebViewConfiguration alloc]init];

 self.wkWebViw = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:config];

 [self.wkWebViw loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webPageUrl]]];

 [self.view addSubview:self.wkWebViw];

 self.userCC = config.userContentController;

 [self.userCC addScriptMessageHandler:self name:@"callOSX"];

 //        JS callFunction    

 [self.userCC addScriptMessageHandler:self name:@"callFunction"];

 // JS  callFunction         , WKScriptMessageHandler                

 #pragma mark  WKScriptMessageHandler delegate
 - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
 {
//      , message.name     (‘    callFunction’), message.body  JS         

 }

//  , VC         handler  
-(void)dealloc
{
    [_userContentController removeScriptMessageHandlerForName:@"callFunction"];
}




//   JS  




       <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">buttonClick</span><span class="hljs-params">(string)</span>{</span>
       <span class="hljs-comment">//JS  OC,     </span>
       <span class="hljs-comment">//(window.webkit.messageHandlers.Method_Name.postMessage(parameterToOC))</span>
              window.webkit.messageHandlers.callFunction.postMessage(string)
        }       
  

좋은 웹페이지 즐겨찾기