全部產品
Search
文件中心

SuperApp:接入操作

更新時間:Oct 29, 2024

EMAS支援Native App接入WindVane小程式容器和uni-app小程式容器,您可根據業務需要,選擇接入的小程式容器類型。本文介紹iOS端接入小程式容器的詳細操作。

前提條件

  • 已擷取Native App的AccessKey、SecretKey和App Code,擷取方式請參見使用者指南應用開放平台(平台營運方) > 設定App章節。

  • 已擷取應用開放平台的環境網域名稱。體驗環境網域名稱為:poc.superapp-intl.com

步驟一、添加依賴

  1. 添加部署的CocoaPods倉庫

    pod repo add superapp-intl-emas-ios-emas-specs https://gitlab-console.superapp-intl.com/emas-ios/emas-specs.git

    pod repo add superapp-intl-emas-ios-emas-specs-thirdpart https://gitlab-console.superapp-intl.com/emas-ios/emas-specs-thirdpart.git
  2. 在工程根目錄的Podfile中,配置CocoaPods倉庫。

    小程式容器相關SDK發布在部署的CocoaPods倉庫上,賬戶密碼可以聯絡營運工程師提供。

    source 'https://gitlab-console.superapp-intl.com/emas-ios/emas-specs.git'
    source 'https://gitlab-console.superapp-intl.com/emas-ios/emas-specs-thirdpart.git'
  3. 在工程根目錄的Podfile中,添加小程式容器的依賴。

    use_frameworks!
    //小程式容器基礎sdk,必須整合
    pod 'EMASServiceManager'
    pod 'EMASMiniAppAdapter'
    //WindVane小程式容器,如果選擇WindVane小程式,則需要整合。
    pod 'EMASWindVaneMiniApp'
    //uni-app小程式容器,如果選擇uni-app小程式,則需要整合。
    pod 'EMASUniappMiniApp'

步驟二、初始化容器

使用小程式容器前,需要初始化小程式容器。

// 初始化SDK
EMASMiniAppInitConfig *miniAppInitConfig = [EMASMiniAppInitConfig new];
miniAppInitConfig.useUniApp = YES;
miniAppInitConfig.useWindVane = YES;
miniAppInitConfig.accessKey = @"ZXZ8****";//從應用開放平台擷取
miniAppInitConfig.secretKey = @"3PNJxKtf7/eqUohlG3bI****";//從應用開放平台擷取
miniAppInitConfig.host = @"emas.example.com";//請配置您的環境網域名稱,此處僅為樣本。體驗環境網域名稱為:emas-publish-intl.emas-poc.com。
miniAppInitConfig.appCode = @"161160615860445224****";//從應用開放平台擷取

EMASMiniAppServiceImpl *miniAppService = [EMASMiniAppServiceImpl new];
[miniAppService initialize:miniAppInitConfig];
[[EMASServiceManager sharedInstance] registerServiceProtocol:@"EMASMiniAppService" IMPClass:@"EMASMiniAppServiceImpl" target:miniAppService];

//下面是WindVane小程式容器的其他配置。若使用需要實現對應類。
//[[EMASServiceManager sharedInstance] registerServiceProtocol:@"EMASMiniAppFavoriteService" IMPClass:@"EMASMiniAppFavoriteServiceImpl" target:[EMASMiniAppFavoriteService new]];//收藏功能
//[[EMASServiceManager sharedInstance] registerServiceProtocol:@"EMASUserInfoService" IMPClass:@"EMASUserInfoServiceImpl" target:[EMASUserInfoServiceImpl new]];//關聯使用者功能
//[[EMASServiceManager sharedInstance] registerServiceProtocol:@"EMASMiniAppURLService" IMPClass:@"EMASMiniAppURLServiceImpl" target:[EMASMiniAppURLServiceImpl new]];// 攔截URL功能

注意事項

  1. 使用Apple Silicon M系列晶片的Mac電腦並使用模擬器運行時,模擬器需要選擇Rosetta標識的模擬器。

    安裝Rosetta,終端運行以下代碼:

    sudo softwareupdate --install-rosetta --agree-to-license

    未使用Rosetta運行報錯Building for 'iOS-simulator',but linking in object file, 需要修改 Build Settings -> Excluded Architectures 選項,添加Any iOS Simulator SDK選項,設定值為arm64。三方庫報錯x86_64 Undefined symbol:_OBJC_CLASS_$_ 需要在工程的 build setting -> Other linker flags中添加 -ld64,之後在Product -> Destination 選擇Rosetta標識的模擬器運行專案。

  2. 在 Xcode15新增ENABLE_USER_SCRIPT_SANDBOXING,遇到報錯Sandbox: rsync.samba deny 修改build setting -> user script sandboxing 為No。

API參考

關於API的詳細說明請參見API參考