JS怎么样调用IOS并且传递参数

JavaScript018

JS怎么样调用IOS并且传递参数,第1张

可以看下javascriptcore框架的文档,这个框架让Objective-C和JavaScript代码直接交互变得非常简单方便。

如果是Cordova,可以通过写扩展的方式。

如果是WebKit,可以通过Cocoa的API。

基本流程:

先看一下Web中,我们给h1标签添加一个onclick事件,让它在被点击之后,修改当前的url。

Web中的HTML代码:

<html>

<head>

<script>

function getInfo(name)

{

window.location = "/getInfo/"+name

}

</script>

</head>

<body>

<h1 onclick="getInfo('why')">Name</h1>

</body>

</html>

iOS中,先拖拽WebView,访问localhost,然后通过WebView的委托事件监听url跳转操作,并且把跳转截取下来。

也就是说,在onclick的时候,普通浏览器灰跳转到那个url,但是在iOS的这个WebView里面,这个跳转会被拦截,

用这种方式可以巧妙地实现JS调用iOS的原生代码:

//

// DWViewController.m

// DareWayApp

//

// Created by why on 14-6-3.

// Copyright (c) 2014年 DareWay. All rights reserved.

//

#import "DWViewController.h"

@interface DWViewController ()

@property (weak, nonatomic) IBOutlet UIWebView *myWebview // 主页面

@end

@implementation DWViewController

- (void)viewDidLoad

{

[super viewDidLoad]

// Do any additional setup after loading the view, typically from a nib.

// 适配iOS6的状态栏

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

_myWebview.frame = CGRectMake(0,20,self.view.frame.size.width,self.view.frame.size.height-20)

}

// 加载制定的URL

NSURL *url =[NSURL URLWithString:@"http://localhost"]

NSURLRequest *request =[NSURLRequest requestWithURL:url]

[_myWebview setDelegate:self]

[_myWebview loadRequest:request]

}

// 网页中的每一个请求都会被触发

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

{

// 每次跳转时候判断URL

if([request.mainDocumentURL.relativePath isEqualToString:@"/getInfo/why"])

{

NSLog(@"why")

return NO

}

return YES

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning]

// Dispose of any resources that can be recreated.

}

@end

在Cocos2d-JS v3.0 RC2中,与Android上js调用Java一样,Cocos2d-JS也提供了在iOS和Mac上js直接调用Objective-C的方法,示例代码如下:

var ojb = jsb.reflection.callStaticMethod(className, methodNmae, arg1, arg2, .....)

在jsb.reflection.callStaticMethod方法中,我们通过传入OC的类名,方法名,参数就可以直接调用OC的静态方法,并且可以获得OC方法的返回值。

参数中的类名,只需要传入OC中的类名即可,与Java不同,类名并不需要路径。比如你在工程底下新建一个类NativeOcClass,只要你将他引入工程,那么他的类名就是NativeOcClass,你并不需要传入它的路径。

+(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content

方法

js到OC的反射仅支持OC中类的静态方法。

方法名比较要需要注意,我们需要传入完整的方法名,特别是当某个方法带有参数的时候,你需要将他的:也带上。根据上面的例子。此时的方法名字是callNativeUIWithTitle:andContent:,不要漏掉了他们之间的:。

如果是没有参数的函数,那么他就不需要:,如下代码,他的方法名是callNativeWithReturnString,由于没有参数,他不需要:,跟OC的method写法一致。

+(NSString *)callNativeWithReturnString

使用示例

下面的示例代码将调用上面NativeOcClass的方法,在js层我们只需要这样调用:

var ret = jsb.reflection.callStaticMethod("NativeOcClass",

"callNativeUIWithTitle:andContent:",

"cocos2d-js",

"Yes! you call a Native UI from Reflection")

这里是这个方法在OC的实现,可以看到是弹出一个native的对话框。并把title和content设置成你传入的参数,并返回一个boolean类型的返回值。

+(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]

[alertView show]

return true

}

此时,你就可以在ret中接受到从OC传回的返回值(true)了。

注意

在OC的实现中,如果方法的参数需要使用float、int、bool的,请使用如下类型进行转换:

float,int 请使用NSNumber类型

bool请使用BOOL类型。

例如下面代码,我们传入2个浮点数,然后计算他们的合并返回,我们使用NSNumber而不是int、float去作为参数类型。

+(float) addTwoNumber:(NSNumber *)num1 and:(NSNumber *)num2{

float result = [num1 floatValue]+[num2 floatValue]

return result

}

目前参数和返回值支持 int, float, bool, string,其余的类型暂时不支持。