Skip to main content

Cocos Adapter

Overview

The Cocos Native Adapter is a dedicated adapter provided by cloud development for the Cocos Creator native platform (iOS/Android). It enables developers to seamlessly use all features of cloud development in the project. With this adapter, you can easily achieve cloud-based data storage, user authentication, file management, and function call on the mobile terminal.

Supported Platforms

Currently adapted platforms:

-All platforms supported by Cocos Creator

Environmental Requirements

  • Node.js >= 22
  • Cocos Creator 3.x
  • @cloudbase/js-sdk >= 2.25.1
  • @cloudbase/adapter-cocos_native >= 1.0.0

Effect Display

For the complete sample project, see CloudBase Cocos Demo

Installation

Install with npm:

npm install @cloudbase/js-sdk @cloudbase/adapter-cocos_native

Quick Start

Adapter Configuration

import cloudbase from "@cloudbase/js-sdk";
import adapter from "@cloudbase/adapter-cocos_native";

Register adapter
cloudbase.useAdapters(adapter);

const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
region: "ap-shanghai", // Region
accessKey: "your-access-key", // Publishable Key
});

export default app;
Important Note

adapter must be called before cloudbase.init()

Features

  • Network request: Implemented with XMLHttpRequest and optimized for Cocos Native platform compatibility
  • Local storage: Use cc.sys.localStorage. sessionStorage is not supported in native environments.
  • AbortController: Built-in polyfill supports request termination without requiring installation.

Environment test

The adapter automatically detects whether the present environment is Cocos Native, with detection criteria including:

  • cc global object exists
  • cc.sys.isNative is true
  • XMLHttpRequest and WebSocket are available

Main dependency

DependencyVersionDescription
@cloudbase/js-sdk>=2.25.1Tencent Cloud Development SDK
@cloudbase/adapter-cocos_native^1.0.0Cocos Native adapter

FAQs

Q: Why call useAdapters before init?

A: The adapter needs to be registered before SDK initialization to ensure the SDK can correctly identify the current runtime environment and use the appropriate adapter.

Q: What are the limits of local storage?

A: Cocos Native environment usage cc.sys.localStorage for local storage. sessionStorage is not supported.

Q: Which versions of Cocos Creator are supported?

A: The adapter supports Cocos Creator 3.x version.

-CloudBase Official Documentation