Once again I have a problem, which I just don't know how to solve. The problem is how I supposed to pass data back to Android and iOS calling applications from my cocos2d-x app?
For Android, I guess I'll use activity results when the app ends with JNI, but how can I achieve this on iOS?
Let's take a not-so-actual example: I have an existing application both on Android and iOS. This application is holding scores for different games, and from this application, you can call other game applications (also written by me). When the called game ends the main application should get the scores back (like an activity result).
If you guys could give me a few pointers, I would really appreciate that.
In android you can do something like this: (Probably in iOS you can do the same :) )
For example i would like to return a class with data.
So i create in
/cocos2d-2.0-x-2.0.4/cocos2dx/platform/android/java/src/com/example
(try to put this class in you project i don't check it)
package com.example;
public class SimpleClass {
private int a;
private int b;
public SimpleClass(int a, int b) {
this.a = a;
this.b = b;
}
}
Now in your Cocos2dxActivity you can add native method which will be executed when you exit or other action it's up to you.
For example
private static native SimpleClass nativeReturner();
and you can execute it in onStop or other method it's up to you. It will return your SimpleClass
Next in your project you should have jni/main.cpp a JNI functions so simply you can add like this function:
jobject Java_org_cocos2dx_lib_Cocos2dxActivity_nativeReturner(JNIEnv* env, jobject obj)
{
jint i;
jobject object;
jmethodID constructor;
jclass cls;
cls = env->FindClass("com/example/SimpleClass");
constructor = env->GetMethodID(cls, "<init>", "(II)V");
//Here you initialize your class pass here something
object = env->NewObject(cls, constructor, 5, 12);
return object;
}
This simple function creates you a SimpleClass and initialize it with 5,12 of course you can pass here your object form you cocos game.
For example in your class that inherits form CCAplication you can store data you would like to pass. Next you can use CCApplication::sharedApplication() or create your own static method.
Of course in iOS and other platform you must to simlar things.