js调用oc
在ios工程中声明定义一个静态方法供js调用
在.h文件中声明函数showAd
#import
@class RootViewController;
@interface AppController : NSObject
{
}
+(NSString *)showAd:(NSString *)str title:(NSString *)tit;
@property(nonatomic, readonly) RootViewController* viewController;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
#import
@classRootViewController;
@interfaceAppController:NSObject
{
}
+(NSString *)showAd:(NSString *)strtitle:(NSString *)tit;
@property(nonatomic,readonly)RootViewController*viewController;
@end
在.mm文件中定义如下
+(NSString *)showAd:(NSString *)str title:(NSString *)tit{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:tit message:str delegate:nil cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
[alertView show];
return @"hehe";
}
1
2
3
4
5
+(NSString *)showAd:(NSString *)strtitle:(NSString *)tit{
UIAlertView *alertView=[[UIAlertViewalloc]initWithTitle:titmessage:strdelegate:nilcancelButtonTitle:@"否"otherButtonTitles:@"是",nil];
[alertViewshow];
return@"hehe";
}
在js脚本中调用方式如下
if (cc.sys.isNative&&cc.sys.os==cc.sys.OS_IOS) {
let ret = jsb.reflection.callStaticMethod("AppController","showAd:title:","title","message");
cc.log(ret)//打印输出:hehe
}
1
2
3
4
if(cc.sys.isNative&&cc.sys.os==cc.sys.OS_IOS){
letret=jsb.reflection.callStaticMethod("AppController","showAd:title:","title","message");
cc.log(ret)//打印输出:hehe
}
调用后oc代码展示了弹框,并显示了传入参数
js代码打印了调用返回值
oc调用js
在js中定义一个全局函数供oc调用
window.testMethod = (str)=>{
cc.log('window.testMethod' , str)
return 'abcd'
}
1
2
3
4
window.testMethod=(str)=>{
cc.log('window.testMethod',str)
return'abcd'
}
oc代码调用方式如下
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
using namespace cocos2d;
@implementation AppController
@synthesize window;
#pragma mark -
#pragma mark Application lifecycle
// cocos2d application instance
static AppDelegate* s_sharedApplication = nullptr;
+(NSString *)showAd:(NSString *)str title:(NSString *)tit{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:tit message:str delegate:nil cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
[alertView show];
// call the js function
std::string strRet = "haha";
std::string jsCallStr = cocos2d::StringUtils::format("testMethod(\"%s\");", strRet.c_str());
se::Value *ret = new se::Value();
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str() , -1 , ret);
NSLog(@"jsCallStr rtn = %s", ret->toString().c_str());
//
return @"hehe";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
usingnamespacecocos2d;
@implementationAppController
@synthesizewindow;
#pragma mark -
#pragma mark Application lifecycle
// cocos2d application instance
staticAppDelegate*s_sharedApplication=nullptr;
+(NSString *)showAd:(NSString *)strtitle:(NSString *)tit{
UIAlertView *alertView=[[UIAlertViewalloc]initWithTitle:titmessage:strdelegate:nilcancelButtonTitle:@"否"otherButtonTitles:@"是",nil];
[alertViewshow];
// call the js function
std::stringstrRet="haha";
std::stringjsCallStr=cocos2d::StringUtils::format("testMethod(\"%s\");",strRet.c_str());
se::Value *ret=newse::Value();
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str(),-1,ret);
NSLog(@"jsCallStr rtn = %s",ret->toString().c_str());
//
return@"hehe";
}
这里修改了showAd方法,增加call the js function相关部分
注意首先引入了SeApi.h文件,从而可以访问命名空间se::
然后构建语句字符串,调用testMethod方法并传入参数
再由ScriptEngine执行
可以看到js代码打印了参数haha
oc代码打印了返回值abcd
js调用java
场景中摆放一个label
点击label调用java中的方法(传入参数),并在label显示该方法的返回结果(字符串)
js代码如下
this.testlabel.node.on('touchend' , ()=>{
var rtn = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "show", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", "title", "message");
this.testlabel.string = rtn
})
1
2
3
4
this.testlabel.node.on('touchend',()=>{
varrtn=jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity","show","(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;","title","message");
this.testlabel.string=rtn
})
java代码中,找到cocoscreator发布安卓工程所生成的AppActivity.java文件
并添加show方法如下
public static String show(String title, String message) {
return title + "--" + message;
}
1
2
3
publicstaticStringshow(Stringtitle,Stringmessage){
returntitle+"--"+message;
}
以上运行时,点击label后更新显示了’title–message’
最后编辑:2020-05-02作者:Nliki
这个作者貌似有点懒,什么都没有留下。