c++ – URL schemes in Cocos Creator-written iOS game

[ad_1]

I am trying to implement URL schemes in my app, which I wrote in Cocos Creator 2.4.5 and then exported to XCode for iOS deployment. The app works fine, but I can’t get URL schemes to work. I think it is a matter of Swift code vs. Objective C (both of which I am not very proficient at, as you can tell from me posting this).

I’m trying to implement something like this

func application(_ application: UIApplication,
                 open url: URL,
                 options: [UIApplicationOpenURLOptionsKey : Any] = [:] ) -> Bool {

    // Determine who sent the URL.
    let sendingAppID = options[.sourceApplication]
    print("source application = \(sendingAppID ?? "Unknown")")

    // Process the URL.
    guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
        let albumPath = components.path,
        let params = components.queryItems else {
            print("Invalid URL or album path missing")
            return false
    }

    if let photoIndex = params.first(where: { $0.name == "index" })?.value {
        print("albumPath = \(albumPath)")
        print("photoIndex = \(photoIndex)")
        return true
    } else {
        print("Photo index missing")
        return false
    }
}


(following this doc: https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app)

in my AppDelegate.cpp:

/****************************************************************************
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

 http://www.cocos.com

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated engine source code (the "Software"), a limited,
 worldwide, royalty-free, non-assignable, revocable and non-exclusive license
 to use Cocos Creator solely to develop games on your target platforms. You shall
 not use Cocos Creator software for developing other software or tools that's
 used for developing games. You are not granted to publish, distribute,
 sublicense, and/or sell copies of Cocos Creator.

 The software or tools in this License Agreement are licensed, not sold.
 Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#include "AppDelegate.h"

#include "cocos2d.h"

#include "cocos/scripting/js-bindings/manual/jsb_module_register.hpp"
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
#include "cocos/scripting/js-bindings/event/EventDispatcher.h"
#include "cocos/scripting/js-bindings/manual/jsb_classtype.hpp"

USING_NS_CC;
#ifdef SDKBOX_ENABLED
#include "PluginIAPJS.hpp"
#include "PluginIAPJSHelper.h"
#endif
#ifdef SDKBOX_ENABLED
#include "PluginReviewJS.hpp"
#include "PluginReviewJSHelper.h"
#endif

AppDelegate::AppDelegate(int width, int height) : Application("Cocos Game", width, height)
{
}

AppDelegate::~AppDelegate()
{
}



bool AppDelegate::applicationDidFinishLaunching()
{
    se::ScriptEngine* se = se::ScriptEngine::getInstance();

    jsb_set_xxtea_key("7d334562-0879-4b");
    jsb_init_file_operation_delegate();

#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
    // Enable debugger here
    jsb_enable_debugger("0.0.0.0", 6086, false);
#endif

    se->setExceptionCallback([](const char* location, const char* message, const char* stack){
        // Send exception information to server like Tencent Bugly.
        cocos2d::log("\nUncaught Exception:\n - location :  %s\n - msg : %s\n - detail : \n      %s\n", location, message, stack);
    });

    jsb_register_all_modules();
#ifdef SDKBOX_ENABLED
    se->addRegisterCallback(register_all_PluginIAPJS);
    se->addRegisterCallback(register_all_PluginIAPJS_helper);
#endif
#ifdef SDKBOX_ENABLED
    se->addRegisterCallback(register_all_PluginReviewJS);
    se->addRegisterCallback(register_all_PluginReviewJS_helper);
#endif

    se->start();

    
    se::AutoHandleScope hs;
    jsb_run_script("jsb-adapter/jsb-builtin.js");

    jsb_run_script("main.js");

    
    se->addAfterCleanupHook([](){
        JSBClassType::destroy();
    });

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::onPause()
{
    EventDispatcher::dispatchOnPauseEvent();
}

// this function will be called when the app is active again
void AppDelegate::onResume()
{
    EventDispatcher::dispatchOnResumeEvent();
}

… which is a cpp file. I can’t find a cpp example for that URL handling, unfortunately.

(Here, I would then write objects into the JS environment in which my game runs:

 se::ScriptEngine::getInstance()->evalString("window.your_func('put string here')");

)

But adding the URL-handling function to my AppDelegate is something I can’t really manage to pull off. Any ideas on this would be fantastic! Thank you!

[ad_2]

Source link

Leave a Reply

Your email address will not be published.