話不多說直接上代碼
1
原生分享帶參數(shù)沒有返回值
js這么寫
function ThirdPartyShareImg(Path, isLine) {
console.log("WXShareTex "+Path )
if(cc.sys.isNative){
if(cc.sys.OS_IOS == cc.sys.os){
jsb.reflection.callStaticMethod("AppController","WXShareTex:IsTimeLine:",Path,isLine);
}else{
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'WXShareTex',
"(Ljava/lang/String;Ljava/lang/String;)V",
Path, isLine);
}
}
}
iOS 這么寫
#pragma mark - ----------------- 微信分享圖片 --------------------
+(void)WXShareTex:(NSString*)content_link IsTimeLine:(NSNumber *)IsTimeLine{
if (IsTimeLine.intValue == 1) {
//1.創(chuàng)建多媒體消息結(jié)構(gòu)體
WXMediaMessage *mediaMsg = [WXMediaMessage message];
//2.創(chuàng)建多媒體消息中包含的圖片數(shù)據(jù)對象
WXImageObject *imgObj = [WXImageObject object];
//圖片真實數(shù)據(jù)
NSString *imageString = content_link;
if([content_link hasPrefix:@"http"])
{//判斷字符串是否以B字符開始
NSLog(@"開頭為字母B");
//1.創(chuàng)建多媒體消息結(jié)構(gòu)體
WXMediaMessage *mediaMsg = [WXMediaMessage message];
//2.創(chuàng)建多媒體消息中包含的圖片數(shù)據(jù)對象
WXImageObject *imgObj = [WXImageObject object];
//圖片真實數(shù)據(jù)
NSURL *url=[NSURL URLWithString:content_link];
imgObj.imageData = [NSData dataWithContentsOfURL:url];
//多媒體數(shù)據(jù)對象
mediaMsg.mediaObject = imgObj;
//3.創(chuàng)建發(fā)送消息至微信終端程序的消息結(jié)構(gòu)體
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
//多媒體消息的內(nèi)容
req.message = mediaMsg;
//指定為發(fā)送多媒體消息(不能同時發(fā)送文本和多媒體消息琼锋,兩者只能選其一)
req.bText = NO;
//指定發(fā)送到會話(聊天界面)
req.scene = WXSceneTimeline;
//發(fā)送請求到微信,等待微信返回onResp
[WXApi sendReq:req];
是不是很簡單
2 沒參數(shù)有返回值
js
function ThirdPartyGetBattery() {
var pLv = 1;
if(cc.sys.OS_IOS == cc.sys.os){
pLv = jsb.reflection.callStaticMethod('AppController',"GetBatteryLv");
}else{
pLv = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity',"GetBatteryLv","()Ljava/lang/String;");
}
return pLv;
}
返回值是NSNumber類型
iOS 這么寫
#pragma mark - ----------------- 電池 --------------------
+(NSNumber *)GetBatteryLv{
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
double deviceLevel = [UIDevice currentDevice].batteryLevel;
NSString *str = [NSString stringWithFormat:@"%.2f", deviceLevel];
NSLog(@"+++++++++++++++++++++++%@", str);
return @([str integerValue]);
}
3 沒有參數(shù)沒有返回值
JS
function ThirdPartyGetAddress() {
console.log('ThirdPartyGetAddress 走了');
if (cc.sys.OS_IOS == cc.sys.os) {
jsb.reflection.callStaticMethod("AppController","getAddress");
} else {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getAddress',"()V");
}
}
iOS
#pragma mark - ----------------- 定位 --------------------
+(void)getAddress{
NSLog(@"123123123");
AMapLocationManager *locationManager = [[AMapLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
[locationManager setLocationTimeout:6];
[locationManager setReGeocodeTimeout:3];
}
以上都是JS 調(diào)OC 方法, 下面介紹OC 調(diào)用JS 方法
微信登錄為例
JS 寫法
onWXCode:function (code) {
console.log("onWXCode "+code)
if(code == ''){
console.log("onWXCode 1")
this.m_WXLoginBt.interactable=true;
}else{
console.log("onWXCode 2" + code);
code = encodeURI(code);
var webUrl = WEB_HEAD + LOGIN_SERVER_IP + '/UserFunc.php?&GetMark=14&code=' + code;
HttpUtils.getInstance().httpGets(webUrl, function (data) {
console.log("onWXCode 3")
var Login = JSON.parse(data);
if(Login.errcode != null) return this.ShowAlert("ErrCode:" + Login.errcode);
this.LoginAccount(Login.Accounts ,Login.LogonPass);
}.bind(this));
console.log("onWXCode 4")
}
},
OC 因為OC 的字符串跟C++不一樣需要進行一下轉(zhuǎn)碼, 而JS 要的字符串格式是json類型的字符串, 所以O(shè)C 這邊需要對字符串進行拼接(當(dāng)然還有其他方法生成json字符串).
NSLog(@"%@", mutStr);
// NSString *str = [NSString stringWithFormat:@"{\"openid\":\"%@\",\"nickname\":\"%@\",\"sex\":\"%@\",\"headimgurl\":\"%@\"}",resp.openid,resp.name,resp.unionGender,resp.iconurl];
NSString *str = [NSString stringWithFormat:@"{\"headimgurl\":\"%@\",\"nickname\":\"%@\",\"sex\":\"%@\",\"openid\":\"%@\"}",resp.iconurl,resp.name,resp.unionGender,resp.openid];
NSLog(@"%@", str);
// std::string strRet1 = "{}";
// NSString * encodingString = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
std::string strRet1 = [str UTF8String];
std::string strRet = "onWXCode";
NSLog(@"QQQQQQQQQ:::::%s",strRet1.c_str());
std::string jsCallStr = cocos2d::StringUtils::format("CallLoginFunc(\"%s\",'%s');", strRet.c_str(),strRet1.c_str());
NSLog(@"~~~~~~~~~~~~~%s",jsCallStr.c_str());
se::Value *ret = new se::Value();
se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str() , -1 , ret);
轉(zhuǎn)json字符串時候有幾個坑, 因為OC 的字符串中不能包含""的 在有""的地方需要加\轉(zhuǎn)義, 但是給JS 發(fā)過去的字符串又因為多幾個"'而獲取不到, 所以在后面C++拼接的時候選擇的是''單引號, 這樣完美的避開了OC雙引號的問題. 當(dāng)然了因為JS 那邊不是我寫的, 其實最好的辦法是JS 那邊不是接一個參數(shù), 而是多個參數(shù), 這樣就不會出現(xiàn)雙引號的問題. 這個問題坑了我一下午