Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

javascript - Couldn't call Objective-C method from Cocos-2d via JavaScriptObjCBridge

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

Above is the documentation to call a Objective-C method from Cocos script

I trying to do the exact same thing, but I get the error

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

Screen Shot 2020-07-21 at 4 05 36 PM

.h class

//
//  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 class

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

#import "TestingLogging.h"

@implementation TestingLogging


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

@end

Script in cocos

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

Could you please help me !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Objective-C the colon is part of the method name. It's mentioned in the linked documentation

methodName parameter in previous example is the OC method name in your class, take NativeOcClass as an example, we can see a method named

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

So the methodName should be callNativeUIWithTitle:addContent: which is the definition for this method, and don't forget the :.

The name of the method is testingIntegration:. Change the script to

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...