Initialize the engine
Android
CubeInitParam cubeInitParam = CubeInitParam.getDefault();
MP.init(this, MPInitParam.obtain().addComponentInitParam(cubeInitParam));iOS
[[CubeService sharedInstance] initWithConfig:config];Initialize engine parameters
Set the preset resource path
Android
CubeEngineConfig config = new CubeEngineConfig();
config.setResourcePath("cube");
cubeInitParam.setCubeEngineConfig(config)iOS
CubeEngineConfig* config = [[CubeEngineConfig alloc] init];
[config setBundlePath:mockBundlePath];// The path to local cards.Set a custom JSAPI
Android
/**
* Registers a Cube JSAPI.
* @param cubeModuleModels
*/
public CubeInitParam setCubeModuleModels(Collection<CubeModuleModel> cubeModuleModels)
cubeInitParam.setCubeModuleModels(generateModuleModel())iOS
NSDictionary *dic = @{@"MethodName": @"ImplementationClassName", @"abc": @"MPDemoModule"};
[[[CubeService sharedInstance] getEngine] registerModules:dic];Set custom tags
Android
/**
* Registers a custom view (custom tag).
* @param cubeWidgetInfos
*/
public CubeInitParam setCubeWidgetInfos(Collection<CubeWidgetInfo> cubeWidgetInfos)
cubeInitParam.setCubeWidgetInfos(generateWidget())iOS
CubeWidgetInfo *widgetInfo = [CubeWidgetInfo new];
widgetInfo.tag = @"custom-widget";
widgetInfo.className = [MPDemoCustomCardView class];
NSMutableArray *widgetArray = [NSMutableArray array];
[widgetArray addObject:widgetInfo];
[[[CubeService sharedInstance] getEngine] registerWidgets:[NSArray arrayWithArray:widgetArray]];Set an image downloader
Android
cubeInitParam.setImageHandler(new ICKImageHandler() {
@Override
public String loadImage(String s, int i, int i1, Map<String, Object> map, LoadImageListener loadImageListener) {
return null;
}
@Override
public void cancel(String s) {
}
iOS
You can use CubeEngineConfig to intercept image downloads and customize image parameters. The client must conform to the CKImageHandler proxy and implement the listener method.
// Properties of the CubeEngineConfig card engine class
/// The image handler. If this is empty, the default internal implementation is used. Customize the handler.
@property (nonatomic, strong) id<CKImageHandler> imageHandler;// CKImageHandler.h
// CubePlatform
// Created by Joe on 2018/8/15.
// Copyright © 2018 mQuick. All rights reserved.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#ifndef CKImageHandler_h
#define CKImageHandler_h
extern NSString *const CKImageAppInstanceIDKey; // The AppInstanceID that corresponds to the triggered image loading.
extern NSString *const CKImagePageInstanceIDKey; // The PageInstanceID that corresponds to the triggered image loading.
extern NSString *const CKImageInstanceOptionKey; // The instance option. Added for the Falcon link.
typedef void(^CKImageHandlerCallback)(UIImage *image, NSError *error);
@protocol CKImageHandler <NSObject>
@required
/**
@param url The image download URL.
@param size The image size.
@param option Extension parameters that depend on the image download library.
@param callback The download callback. This callback is triggered after the download is complete.
@return Returns the unique ID of the download task.
*/
- (NSString *)fetchImage:(NSString *)url size:(CGSize)size option:(NSDictionary *)option callback:(CKImageHandlerCallback)callback;
@optional
/**
@param fetchID The task ID, which is the return value of fetchImage.
*/
- (void)cancel:(NSString*)fetchID;
@end@interface MPCubeManager () <CKImageHandler>
- (void)initEngine {
CubeEngineConfig *engineConfig = [[CubeEngineConfig alloc] init];
// Set the delegate.
engineConfig.imageHandler = self;
[[CubeService sharedInstance] initWithConfig:engineConfig];
}
- (NSString *)fetchImage:(NSString *)url size:(CGSize)size option:(NSDictionary *)option callback:(CKImageHandlerCallback)callback {
NSLog(@"Image URL: %@", url);
NSLog(@"Image extension parameters: %@", option);
return @"20211202";
}Set an exception listener
Android
cubeInitParam.setExceptionListener(new CExceptionListener() {
@Override
public void onException(CExceptionInfo cExceptionInfo) {
}
})iOS
You can use CubeEngineConfig to listen for and catch frontend code exceptions. The client must conform to the CExceptionListener proxy and implement the listener method.
// Properties of the CubeEngineConfig card engine class
/// Exception listener
@property (nonatomic, strong) id<CExceptionListener> exceptionListener;// CExceptionListener proxy class
// CrystalExceptionProtocol.h
// CubeCrystal
// Created by hejin on 2021/9/10.
#import <Foundation/Foundation.h>
#import "CExceptionInfo.h"
#ifndef CExceptionListener_h
#define CExceptionListener_h
@protocol CExceptionListener <NSObject>
@required
/**
Exception listener
@param info The exception information.
*/
- (void)onException:(CExceptionInfo *)info;
@end@interface MPCubeManager () <CExceptionListener>
- (void)initEngine {
CubeEngineConfig *engineConfig = [[CubeEngineConfig alloc] init];
// Set the delegate.
engineConfig.exceptionListener = self;
[[CubeService sharedInstance] initWithConfig:engineConfig];
}
// Implement the listener method to print listener information.
- (void)onException:(CExceptionInfo *)info {
NSLog(@"Exception type: %lu", (unsigned long)info.type);
NSLog(@"Exception message: %@", info.message);
NSLog(@"Exception card ID: %@", info.cardUid);
NSLog(@"Exception information extension parameters: %@", info.extraInfo);
}Set a log listener
Android
cubeInitParam.setLogHandler(new ICKLogHandler() {
@Override
public void log(Context context, int i, String s, String s1, Throwable throwable) {
// Android log
}
@Override
public void jsLog(Context context, String s) {
// JS log
}iOS
CubeEngineConfig* config = [[CubeEngineConfig alloc] init];
config.logHandler = self;
[[CubeService sharedInstance] initWithConfig:config];
#pragma mark - CKLogHandler
- (void)nativeLog:(NSString *)log type:(CKLogType)type {
}
- (void)jsLog:(NSString *)log {
}
Set an initialization callback
Android
cubeInitParam.setCubeInitCallback(new CubeInitParam.CubeInitCallback() {
@Override
public void onInit() {
}
@Override
public void onError(Exception e) {
}
})iOS
Not supported.
Set the RpcHandler
Android
CubeInitParam cubeInitParam
= CubeInitParam.getDefault()
···
.setRpcHandler(new ICRpcHandler() {
@Override
public CKTemplateInfo.CKTemplateInfoResponse fetchTemplateInfo(List<CKTemplateInfo.CKTemplateInfoRequestParam> list) {
// CKRpcHandler() is the basic mPaaS implementation. Customers can extend it in ICRpcHandler.
return null;
}
})iOS
// 1. Set the rpcHandler.
CubeEngineConfig *engineConfig = [[CubeEngineConfig alloc] init];
engineConfig.rpcHandler = self;
[[CubeService sharedInstance] initWithConfig:engineConfig];
// 2. Implement CTemplateInfoProtocol. For the return value, see the log printed during the default RPC request:
// rpc: templateInfo rpc result. You can also use the internal logic of the software development kit (SDK) when you implement your own RPC.
- (id)getTemplateInfoFromRpc:(NSArray *)params {
// 1. Use the custom RPC logic.
// 2. Use the default internal RPC logic of the SDK.
Class clazz = NSClassFromString(@"CrystalTemplateInfoHandler");
if (clazz) {
id<CTemplateInfoProtocol> handler = [[clazz alloc] init];
return [handler getTemplateInfoFromRpc:params];
}
return @{};
}