【问题标题】:Couldn't call Objective-C method from Cocos-2d via JavaScriptObjCBridge无法通过 JavaScript ObjC Bridge 从 Cocos2d 调用 Objective-C 方法
【发布时间】:2020-11-10 13:54:44
【问题描述】:

网址:https://github.com/cocos-creator/creator-docs/blob/master/en/advanced-topics/oc-reflection.md

以上是Cocos脚本调用Objective-C方法的文档

我试图做完全相同的事情,但我得到了错误

2020-07-21 15:56:01.094441+0800 hello_world-mobile[63684:4206926] TestingLogging.testingIntegration method isn't found!
[ERROR] (/Applications/CocosCreator/Creator/2.3.3/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/manual/JavaScriptObjCBridge.mm, 418): call (TestingLogging.testingIntegration) failed, result code: -3
[ERROR] Failed to invoke JavaScriptObjCBridge_callStaticMethod, location: /Applications/CocosCreator/Creator/2.3.3/CocosCreator.app/Contents/Resources/cocos2d-x/cocos/scripting/js-bindings/manual/JavaScriptObjCBridge.mm:427

.h 类

//
//  TestingLogging.h
//  hello_world-mobile
//
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface TestingLogging : NSObject

+(void) testingIntegration: (NSString *) string;

@end

NS_ASSUME_NONNULL_END

.m 类

//
//  TestingLogging.m
//  hello_world-mobile
//
//

#import "TestingLogging.h"

@implementation TestingLogging


+(void)testingIntegration:(NSString *)string {
    NSLog(@"First ios call from cocos creator app %@", string);
}

@end

cocos 中的脚本

onCameraClickHandler () {
    if(cc.sys.OS_IOS == cc.sys.os) {           
        var ret = jsb.reflection.callStaticMethod("TestingLogging", "testingIntegration", "Hello iOS")
    }
}

你能帮帮我吗!

【问题讨论】:

    标签: javascript objective-c cocos2d-js


    【解决方案1】:

    在 Objective-C 中,冒号是方法名的一部分。它在链接文档中提到

    上例中的

    methodName参数是你类中的OC方法名,以NativeOcClass为例,我们可以看到一个名为

    的方法
    +(BOOL)callNativeUIWithTitle:(NSString *)title andContent:(NSString *)content;
    

    所以 methodName 应该是 callNativeUIWithTitle:addContent: 这是这个方法的定义,不要忘记 :.

    方法的名称是testingIntegration:。将脚本更改为

    var ret = jsb.reflection.callStaticMethod("TestingLogging", "testingIntegration:", "Hello iOS")
    

    【讨论】:

    • 你以前用过 Cocos2d 吗?我想知道我们是否可以将框架直接导入到 cocos creator IDE 中。
    • @KuldeepBhimte 不,我不熟悉 Cocos2d。请提出一个新问题。
    • 我的问题是针对 Cocos2d 的。不管怎样,谢谢你的回答
    猜你喜欢
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多