cocos2d lua调用OC代码

来源:互联网 发布:系统集成软件 编辑:程序博客网 时间:2024/06/06 02:48

1 . 新建LuaCallOC.h 和 LuaCallOC.mm

luaCallOC.h

@interface LuaCallOC : NSObject {}// 跳转到appstore+ (char) gotoAppstore:(NSDictionary *)dict;// 是否WIFI+ (char) IsEnableWIFI;// 获取网络状态+ (int) getNetStatus;@end

luaCallOC.mm

#import "LuaCallOC.h"#import "Reachability.h"#include <arpa/inet.h>@implementation LuaCallOC// 跳转到appstore+ (char) gotoAppstore:(NSDictionary *)dict {    NSString *str = [dict objectForKey:@"address"];    [[UIApplication sharedApplication] openURL:[NSURL URLWithString : str]];    return YES;}// 是否WIFI+ (char) IsEnableWIFI {    struct sockaddr_in addr;    bzero(&addr, sizeof(addr));    addr.sin_len = sizeof(addr);    addr.sin_family = AF_INET;    addr.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);    BOOL ret =([[Reachability reachabilityWithAddress: (sockaddr*)&addr] currentReachabilityStatus] == ReachableViaWiFi);    return ret;}// 获取网络状态+ (int) getNetStatus {    //可以使用多种方式初始化    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com/"];    /*//判断当前的网络状态    switch ([reach currentReachabilityStatus]) {        case ReachableViaWWAN:            NSLog(@"正在使用移动数据网络");            break;        case ReachableViaWiFi:            NSLog(@"正在使用WiFi");            break;        default:            NSLog(@"无网络");            break;    }*/    return [reach currentReachabilityStatus];}@end

2 . 将LuaCallOC.h 和 LuaCallOC.mm添加到xcode
3 .lua调用OC

local ok, ret = luaoc.callStaticMethod("LuaCallOC", "gotoAppstore", {address = markConf[channelId].markAddress})local ok, ret = luaoc.callStaticMethod("LuaCallOC", "IsEnableWIFI")-- ok 为true代表调用成功, 此时ret为返回值-- ok 为false代表调用失败, 此时ret为失败原因
0 0