JavaScript和Lua两者分别调用Native OC接口通道,实现这两个框架的协调工作。
@H_404_2@H5+ SDK以插件的实现客户化的调用Native OC,Lua则可以通过tolua工具实现。开发H5+ SDK插件
@H_404_2@插件名称:PGPluginTest @H_404_2@PluginTest.h @H_404_2@@interfacePGPluginTest : PGPlugin @H_404_2@- (void)gotoMyGame:(PGMethod*)commands;
@H_404_2@+ (void) back2App; @H_404_2@@end @H_404_2@PluginTest.mm @H_404_2@@implementationPGPluginTest- (void)gotoMyGame:(PGMethod*)commands
{
NSString* cbId = [commands.arguments objectAtIndex:0];
// 用户的参数会在第二个参数传回
NSString* pArgument1 = [commands.arguments objectAtIndex:1];
NSLog(@"gotomygame>>>:%@",pArgument1);
NSString* pResultString = @"success";
// 运行Native代码结果和预期相同,调用回调通知JS层运行成功并返回结果
PDRPluginResult *result = [PDRPluginResult resultWithStatus:PDRCommandStatusOK messageAsString:pResultString];
AppManager* mgr = [AppManager getInstance];
mgr.gameName = [NSString stringWithFormat:@"%@%@%@",@"games/",pArgument1,@"/src/main.lua" ];
mgr.ccView = [[CocosViewController alloc] init];
mgr.h5View = [[mgr.navigationController viewControllers] objectAtIndex:0];
[mgr.navigationController pushViewController:mgr.ccView animated:YES];
[self toCallback:cbId withReslut:[result toJSONString]];
}
+ (void) back2App
{
[[AppManager getInstance] releaseCocos];
@H_404_2@}实现C++ tolua
@H_404_2@LuaHelper.mmint LuaHelper::back2App()
{
CCLOG("LuaHelper back2App");
[PGPluginTest back2App];
return 1;
@H_404_2@} @H_404_2@GameUtil.h#ifndef __GAME_UTIL_H_
#define __GAME_UTIL_H_
extern "C" {
#include "lua.h"
#include "tolua++.h"
}
#include "tolua_fix.h"
TOLUA_API int luaopen_GameUtil_luabinding(lua_State* tolua_S);
@H_404_2@#endif @H_404_2@ @H_404_2@GameUtil.cpp#include "GameUtil.h"
#include "LuaHelper.h"
static void tolua_reg_types (lua_State* tolua_S)
{
tolua_usertype(tolua_S,"LuaHelper");
}
#ifndef TOLUA_DISABLE_tolua_HelperFunc_luabinding_HelperFunc_getFileData00
static int tolua_LuaHelper_luabinding_LuaHelper_back2App00(lua_State* tolua_S)
{
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (
!tolua_isusertable(tolua_S,1,"LuaHelper",&tolua_err) ||
!tolua_isstring(tolua_S,2,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
LuaHelper::back2App();
}
return 1;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'back2App'.",&tolua_err);
return 0;
#endif
}
#endif
TOLUA_API int tolua_GameUtil_luabinding_open (lua_State* tolua_S)
{
tolua_open(tolua_S);
tolua_reg_types(tolua_S);
tolua_cclass(tolua_S,"",NULL);
tolua_beginmodule(tolua_S,"LuaHelper");
tolua_function(tolua_S,"back2App",tolua_LuaHelper_luabinding_LuaHelper_back2App00);
tolua_endmodule(tolua_S);
return 1;
}
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
TOLUA_API int luaopen_GameUtil_luabinding (lua_State* tolua_S) {
return tolua_GameUtil_luabinding_open(tolua_S);
};
@H_404_2@#endif @H_404_2@ @H_404_2@AppDelegate.mm @H_404_2@quick_module_register方法调用luaopen_GameUtil_luabinding(L)以便于Lua调用C++接口窗口管理
@H_404_2@关系图如下: @H_404_2@AppManager.h:为了管理整个应用的所有UIWindow和UIController的加载和移除。static AppManager *instance=nil;
@implementation AppManager
@synthesize gameName;
+ (AppManager*)getInstance{
static dispatch_once_tonceToken ;
dispatch_once(&onceToken,^{
instance = [[super allocWithZone:NULL] init] ;
}) ;
return instance;
}
+(id) allocWithZone:(struct _NSZone *)zone
{
return [AppManagerinstance] ;
}
-(id) copyWithZone:(struct _NSZone *)zone
{
return [AppManagerinstance] ;
}
//释放cocos所有变量
- (void)releaseCocos{
cocos2d::CCDirector* director = cocos2d::CCDirector::sharedDirector();
director->end();
[self.ccView.window resignKeyWindow];
[self.ccView.window removeFromSuperview];
self.ccView.window.rootViewController = nil;
self.ccView.window.hidden=YES;
[self.ccView.window release];
[self.navigationController popToViewController:self.h5View animated:YES];
self.ccView.window=nil;
self.ccView=nil;
[self performSelector:@selector(releaseLuaEngine) withObject:nil afterDelay:0.1];
//[self.navigationController.parentViewController makeKeyAndVisible];
}
- (void)releaseLuaEngine{
cocos2d::ScriptEngineManager::getInstance()->destroyInstance();
}
@end