环信SDK Swift工程引入CocoaPods私有化 改造记录

3,819 阅读2分钟

文章背景

环信3.x系列SDK集成遇到的问题记录「遇到其他SDK类似改造也可以」

在集成到Swift项目中,无法直接使用远端仓库进行私有化部署的配置设定

  • 官网人员回复:只提供SDK,对于Podspec和私有化改造需要自己使用方去做。

文章内容大纲

  • 解决Swift工程:私有化配置设定无法引用问题

    • 仅供参考
  • 解决HyphenateChat 组件公司内部仓库CocoaPods托管引用

解决Swift工程:私有化配置设定无法引用问题

  • HyphenateChat.framework 文件组成如下
.
├── Headers
├── HyphenateChat
├── Info.plist
└── Modules

2 directories, 2 files
  • HyphenateChat.h 暴露可访问文件如下
//  HyphenateChat.h
//  HyphenateChat
#import <HyphenateChat/EMClient.h>
#import <HyphenateChat/EMClientDelegate.h>
#import <HyphenateChat/EMMultiDevicesDelegate.h>

可访问及调用 EMOptions+PrivateDeploy的属性及设定,完成如下设置

官方私有云配置说明:docs-im.easemob.com/im/ios/othe…

因为默认未引入,Swift无法直接使用 ,解决思路为如下二个方案

  • 【1】:Swift工程新建分类,对应restServer,chatServer等属性

    • 如果其他工程也使用,则需要N次新增配置,不能一次性解决问题。
  • 【2】:改造SDK,不适用远端版本

    • 通过Pod hook 处理安装后头文件配置(新增EMOptions+PrivateDeploy引用)
    • 直接修改HyphenateChat.h新增配置引用

	  let emOptions = EMOptions(appkey:"Key")
        emOptions?.restServer = "https://private.easemob.com"
        emOptions?.chatServer = "private-previe.easemob.com"
        emOptions?.chatPort = 000
        emOptions?.logLevel = EMLogLevelWarning
        EMClient.shared().initializeSDK(with: emOptions)

参考解决方案如下

HyphenateChat.h 暴露可访问文件新增EMOptions+PrivateDeploy(后续其他项目引用此组件,均可使用,无需其他工程再次配置)

  • 其他未公开能力也一样方式解决。
#import <HyphenateChat/EMClient.h>
#import <HyphenateChat/EMClientDelegate.h>
#import <HyphenateChat/EMMultiDevicesDelegate.h>
#import <HyphenateChat/EMOptions+PrivateDeploy.h>

解决HyphenateChat 组件公司内部仓库CocoaPods托管引用

背景如下

  • 采用Pod方式引入,当前无HyphenateChat.Podspec 描述文件,官方不提供需要自己解决。

解决方案如下

  • 任意工程正常方式引入HyphenateChat
  • 执行此命令:pod cache list
HyphenateChat:
  - Version: 3.8.7
    Type:    External
    Spec:    /Users/dragonli/Library/Caches/CocoaPods/Pods/Specs/External/HyphenateChat/2943ed8b75a4403c3e6452f8bc46b87e.podspec.json
    Pod:     /Users/dragonli/Library/Caches/CocoaPods/Pods/External/HyphenateChat/1c4a1511a149c9179b56aabe82e1dba9-07c43
  • 找到HyphenateChat/2943ed8b75a4403c3e6452f8bc46b87e.podspec.json文件,内容如下
{
  "name": "HyphenateChat",
  "version": "3.8.7",
  "summary": "An Objective-C client for IM service",
  "description": "HyphenateChat is a cloud-based PaaS (Platform as a Service) for Mobile Instant Messaging (MIM). We provide in-app messaging features such as one-to-one chat, group chat, voice message, picture/video/file sharing, location sharing, real-time voice/video calling, etc.",
  "homepage": "http://www.easemob.com/",
  "license": {
    "type": "MIT",
    "file": "LICENSE"
  },
  "authors": {
    "IM SDK ": "DragonLi@163.com"
  },
  "source": {
    "git": "git@192.168.xx/iOS/Hyphenate.git",
    "tag": "3.8.7"
  },
  "platforms": {
    "ios": "11.0"
  },
  "pod_target_xcconfig": {
    "VALID_ARCHS": "arm64 x86_64",
    "EXCLUDED_ARCHS[sdk=iphonesimulator*]": "arm64"
  },
  "user_target_xcconfig": {
    "EXCLUDED_ARCHS[sdk=iphonesimulator*]": "arm64"
  },
  "xcconfig": {
    "OTHER_LDFLAGS": "-ObjC"
  },
  "ios": {
    "vendored_frameworks": "Framework/3.8.7/*.framework"
  }
}

  • 新建一个HyphenateChat.podspec, 参考上述描述,对应编写为如下形式即可(下述可直接使用,改造为对应的Repo地址即可)
Pod::Spec.new do |s|
  s.name             = 'HyphenateChat'
  s.version          = '3.8.7'
  s.summary          = 'An Objective-C client for IM service'
  s.description      = "HyphenateChat is a cloud-based PaaS (Platform as a Service) for Mobile Instant Messaging (MIM). We provide in-app messaging features such as one-to-one chat, group chat, voice message, picture/video/file sharing, location sharing, real-time voice/video calling, etc."
  s.homepage         = 'http://www.easemob.com/'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'IM SDK ' => 'DragonLi@163.com' }
  s.source           = { :git => 'git@192.168.xx.xx/iOS/HyphenateChat.git', :tag => s.version.to_s }
  
  s.ios.deployment_target = '11.0'

  s.pod_target_xcconfig = {
    'VALID_ARCHS' => 'arm64 x86_64',
    'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64'
  }

  s.user_target_xcconfig = {
    'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64'
  }
  
  s.xcconfig = {
    'OTHER_LDFLAGS' => '-ObjC'
  }
  
  s.ios.vendored_frameworks = 'Framework/3.8.7/*.framework'

end