OC和JS之間的交互web
目錄app
對OC和JS之間交互的理解url
JS調用OCspa
JS文件代理
function sendCommand(cmd,param){code
var url = "testapp:"+cmd+":"+param;ci
document.location = url;cmd
}string
function testAction(){it
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