文档

初始化引擎扩展

更新时间:

本文介绍了在 iOS 客户端中初始化引擎扩展的操作步骤。

前置条件

操作步骤

  1. 设置预置卡片本地路径。

    • 设置卡片引擎类 CubeEngineConfig 的属性。

      // 卡片引擎类 CubeEngineConfig 的属性
      
      /// 存储模版的本地资源包的路径
      @property (nonatomic, strong) NSString *bundlePath;
    • 设置本地蚂蚁动态卡片的 assets 路径。

      - (void)initEngine {
          //本地模板所在的 Bundle 路径
          NSString *bundlePath = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], @"MPCubeBundle.bundle"];
          CubeEngineConfig *config = [[CubeEngineConfig alloc] init];
          //设置读取资源路径
          [config setBundlePath:bundlePath];
          //配置卡片引擎
          [[CubeService sharedInstance] initWithConfig:config];
      }
  2. 设置异常监听。

    CubeEngineConfig 支持异常监听,捕获前端代码异常。客户端需要遵循 CExceptionListener 代理,实现监听方法。

    // 卡片引擎类 CubeEngineConfig 的属性
    
    /// 异常监听
    @property (nonatomic, strong) id<CExceptionListener> exceptionListener;
    // CExceptionListener 代理类
    
    //  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
    
    /**
     异常监听
     @param info 异常信息
     */
    - (void)onException:(CExceptionInfo *)info;
    
    @end
    @interface MPCubeManager () <CExceptionListener>
    
    - (void)initEngine {
        CubeEngineConfig *engineConfig = [[CubeEngineConfig alloc] init];
        //设置 delegate
        engineConfig.exceptionListener = self;
        [[CubeService sharedInstance] initWithConfig:engineConfig];
    }
    
    //实现监听方法,打印监听信息
    - (void)onException:(CExceptionInfo *)info {
        NSLog(@"异常类型:%lu", (unsigned long)info.type);
        NSLog(@"异常信息:%@", info.message);
        NSLog(@"异常卡片 id:%@", info.cardUid);
        NSLog(@"异常信息扩展参数:%@", info.extraInfo);
    }
  3. 设置网络图片下载 Handler。

    CubeEngineConfig 支持拦截 Image 下载,定制图片参数;客户端需要遵循 CKImageHandler 代理,实现监听方法。

    // 卡片引擎类 CubeEngineConfig 的属性
    
    /// 图片 handler,如果为空,则内部默认实现,建议自定义 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; // 触发加载图片对应的 AppInstanceID
    extern NSString *const CKImagePageInstanceIDKey; // 触发加载图片对应的 PageInstanceID
    extern NSString *const CKImageInstanceOptionKey; // instance option. 针对 falcon 链路添加
    
    typedef void(^CKImageHandlerCallback)(UIImage *image, NSError *error);
    
    @protocol CKImageHandler <NSObject>
    
    @required
    /**
     @param url 图片下载 URL
     @param size 图片尺寸
     @param option 扩展参数,视图片下载库需求而定
     @param callback 下载回调,下载完成后通过此接口回调
     @return 返回表示本次下载任务的唯一 ID
     */
    - (NSString *)fetchImage:(NSString *)url size:(CGSize)size option:(NSDictionary *)option callback:(CKImageHandlerCallback)callback;
    
    @optional
    /**
     @param fetchID 任务 ID, fetchImage 的返回值
     */
    - (void)cancel:(NSString*)fetchID;
    
    @end
    @interface MPCubeManager () <CKImageHandler>
    
    - (void)initEngine {
        CubeEngineConfig *engineConfig = [[CubeEngineConfig alloc] init];
        //设置 delegate
        engineConfig.imageHandler = self;
        [[CubeService sharedInstance] initWithConfig:engineConfig];
    }
    
    - (NSString *)fetchImage:(NSString *)url size:(CGSize)size option:(NSDictionary *)option callback:(CKImageHandlerCallback)callback {
        NSLog(@"图片地址:%@", url);
        NSLog(@"图片扩展参数:%@", option);
    
        return @"20211202";
    }
    
  • 本页导读 (0)
文档反馈