javascript如何调用objective-c的方法
http://www.cocoachina.com/bbs/read.php?tid=79243&page=e&#a
如题~想要实现 javascript与objc的交互
如何实现javascript调用objc内部的某一个接口呢?
我是这样实现的
例如某一入口
<input type="button" id="enter" value="enter" onclick="testClick('printLog1:Log2:','ddddd','aaaaa');"/>
js里面
function testClick(cmd,parameter1,parameter2)
{
alert(1);
document.write(Date());
window.location.href="objc://"+cmd+":/"+parameter1+":/"+parameter2;
}
注意,window.location.href="objc://"+cmd+":/"+parameter1+":/"+parameter2; // cmd是方法名称不要有空格,后者是附带的参数
其中参数的格式按照自己的需要来定
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *urlString = [[request URL] absoluteString];
NSArray *urlComps = [urlString componentsSeparatedByString:@"://"];
if([urlComps count] && [[urlComps objectAtIndex:0] isEqualToString:@"objc"])
{
NSArray *arrFucnameAndParameter = [(NSString*)[urlComps objectAtIndex:1] componentsSeparatedByString:@":/"];
NSString *funcStr = [arrFucnameAndParameter objectAtIndex:0];
if (1 == [arrFucnameAndParameter count])
{
// 没有参数
if([funcStr isEqualToString:@"doFunc1"])
{
/*调用本地函数1*/
NSLog(@"doFunc1");
}
}
else
{
//有参数的
if([funcStr isEqualToString:@"printLog1:Log2:"])
{
[self printLog1:[arrFucnameAndParameter objectAtIndex:1] Log2:[arrFucnameAndParameter objectAtIndex:2]];
}
}
return NO;
}