1.ios 调用js方法
//定义一个字符串,字符串为js函数的名字,用于调用js
NSString *JSCallBack = [NSString stringWithFormat:@"notify('%@')",message];
[self.viewController.webView stringByEvaluatingJavaScriptFromString:JSCallBack];
notify 为JS端的方法,并且可以传message信息过去
2.js调用ios方法
NSString *a = @"a";
NSString *b = @"b";
NSString *jsString = [NSString stringWithFormat:@"if ( undefined == window.Device ) Device = {}; Device.getDeviceId = function() { return '%@' }; Device.getLocalURI = function() { return '%@' } ",a,b];
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
JS可以通过window.Device.getDeviceId() 调用来获取ios这边的a值,通过window.Device.getLocalURI() 调用来获取ios这边的b值,这样其实是给JS端添加方法
if ( undefined == window.Device ) Device = {};
Device.getDeviceId = function() { return 'a' };
Device.getLocalURI = function() { return 'b' }