OC和JS之间的交互

OC和JS之间的交互

 

目录

  • 对OC和JS之间交互的理解
  • JS调用OC
  • OC调用JS

 

对OC和JS之间交互的理解

 

JS调用OC

JS文件

function sendCommand(cmd,param){

  var url = "testapp:"+cmd+":"+param;

  document.location = url;

}

function testAction(){

  sendCommand("alert","nihao!");

}

OC文件

需要实现的代理

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

  NSString *requestString = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

  if([requestString hasPrefix:@"testapp:"]){

    NSLog(@"JS调用OC");

  }

}

  

OC调用JS

原文地址:https://www.cnblogs.com/IOS-Developer/p/4182817.html