Hello!
I want to be able to create deep links to my app, via a URL scheme, such as
https://developer.apple.com/documentation/xcode/allowing_apps_and_websites_to_link_to_your_content/defining_a_custom_url_scheme_for_your_app
and
https://developer.android.com/training/app-links/deep-linking
– what would be the strategy to implement this in Cocos Creator (i.e. how can I retrieve the parameters given to the URL scheme once the app has been opened?)?
Thanks
Fabian
Hello! Are there any ideas on how to achieve this?
What is your cocos creator version? I think the documents you provide is very clear about application access, not sure what your confusion is?
In version 2.4, you need to add your function code to the source code below
iOS:
cocos2d-x-lite/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm
Android:
cocos2d-x-lite/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/src/org/cocos2dx/javascript/AppActivity.java
if you need pass params to JavaScript, you can use this function( called after engine initialize finish).
iOS:
se::ScriptEngine::getInstance()->evalString()
android :
Cocos2dxJavascriptJavaBridge.evalString()
1 Like
Thanks, that sounds good!
One question:
Once I have my code in AppController.mm ,
how do I set a global variable in my Game?
(Say, if I wanted to set “window.myvariable”, which should be available from my game’s main JS script.)
Thank you!
I’m trying to implement the iOS deep links.
My first question: When I add
continueUserActivity:(NSUserActivity *)userActivity
to my main method:
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {[[SDKWrapper getInstance] application:application didFinishLaunchingWithOptions:launchOptions];
… the app crashes. What am I doing wrong?
(I took this as a template for making userActivity available here, as I needed to find an Objective-C (and not Swift) example: iOS Tutorial => Setup iOS Application (Enabling Universal Links))
My second question: my plan would then be to use this code to examine the URL used:
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[SDKWrapper getInstance] application:application didFinishLaunchingWithOptions:launchOptions] ;
// Add the view controller's view to the window and display.
float scale = [[UIScreen mainScreen] scale];
CGRect bounds = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame: bounds];
///Checking whether the activity was from a web page redirect to the app.
if ([userActivity.activityType isEqualToString: NSUserActivityTypeBrowsingWeb]) {
///Getting the URL from the UserActivty Object.
NSURL *url = userActivity.webpageURL;
NSLog(@"Value of string is %@", url);
if ([url.pathComponents containsObject:@"start"]) {
NSLog(@"Contains start.");
}
}
– is that the right approach?
My third question: How do I then get the data from the URL (once I extracted it) to my game’s main JS?
As always, thank you very much!
All the best,
Fabian
I’ll ask engineering to help.
Thank you, looking forward!
I believe @huanxinyin is already aware of this question
Thanks for looking into this.
I will make sure this is looked into when the engineering team returns from national holiday.
You need to include the header file, and then call the corresponding function, you can refer to the following source code.
cocos2d-x/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp
for example:
se::ScriptEngine::getInstance()->evalString("window.your_func('put string here')");