CocoaPods发布SDK

3,574 阅读4分钟

引言

年前升级基础组件的时候发现百度地图、友盟等都可以使用CocoaPods自动配置.framework形式开发包了。 那叫一个爽啊! 最近接到将公司App产品的基础组件以SDK的方式开放出去供合作方调用的需求。我在第一时间想到了使用CocoaPods发布SDK,在实施过程中还是遇到了一些小坑好在最终都解决了。 希望这篇文章能帮助到有同样需求的你。

准备需要共享的.framework静态库文件

静态库项目编译后分别输出了真机和模拟器的库文件,我写了个简单的脚本在编译后自动合并真机和模拟器库文件。 在build phase中添加runscript。脚本如下: # define output folder environment variable UNIVERSAL_OUTPUTFOLDER={BUILD_DIR}/{CONFIGURATION}-universal

# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"

# touch. copy the RKBluetoothLE_iOS.framework. Just for convenience
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/RKBluetoothLE_iOS.framework" "${UNIVERSAL_OUTPUTFOLDER}/"

# Create universal binary file using lipo
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/RKBluetoothLE_iOS.framework/RKBluetoothLE_iOS" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/RKBluetoothLE_iOS.framework/RKBluetoothLE_iOS" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/RKBluetoothLE_iOS.framework/RKBluetoothLE_iOS"

cmd+B,搞定。 在products/Debug-universal文件夹中找到生成的通用库。

用lipo查看. framework库所支持的架构。

lipo -info RKBluetoothLE_iOS
Architectures in the fat file: RKBluetoothLE_iOS are: x86_64 armv7 arm64 

使用CocoaPods 管理.framework静态库文件

创建SDK项目

CocoaPods提供了pod lib create命令创建库项目

zhijianuandeMBP:~ YuanZhiJian$ pod lib create 'RKBluetoothLEKit'
Cloning `https://github.com/CocoaPods/pod-template.git` into `RKBluetoothLEKit`.
Configuring RKBluetoothLEKit template.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: 
 - http://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )


What language do you want to use?? [ Swift / ObjC ]
 > ObjC

Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > None

Would you like to do view based testing? [ Yes / No ]
 > No

What is your class prefix?
 > RK

将上面生成的RKBluetoothLE_iOS. framework拷贝到生成的RKBluetoothLEKit 库项目的RKBluetoothLEKit目录下

编辑库的Spec描述文件RKBluetoothLEKit.podspec

Pod::Spec.new do |s|
  s.name             = "RKBluetoothLEKit"
  s.version          = "0.1.0"
  s.summary          = "车精灵中控蓝牙SDK"
  s.description      = <<-DESC
车精灵中控蓝牙SDK,提供车辆操控、个性化、自检等功能。API调用模式使用了RAC
                       DESC

  s.homepage         = "https://github.com/yuanzj/RKBluetoothLEKit"
  # s.screenshots     = "www.example.com/screenshots_1", "www.example.com/screenshots_2"
  s.license          = 'MIT'
  s.author           = { "yuanzj" => "jswxyzj@qq.com" }
  s.source           = { :git => "https://github.com/yuanzj/RKBluetoothLEKit.git", :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'
  s.platform     = :ios, '8.0'
  s.requires_arc = true

  # s.source_files = 'RKBluetoothLEKit/Classes/**/*'
  
  # s.resource_bundles = {
  #   'RKBluetoothLEKit' => ['RKBluetoothLEKit/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  s.frameworks = 'Foundation', 'CoreBluetooth'
  s.dependency 'ReactiveCocoa', '~> 4.1.0'
  s.dependency 'CocoaSecurity', '~> 1.2.4'
  s.vendored_frameworks = 'RKBluetoothLEKit/*.{framework}'
  
end

上面最重要的是

s.vendored_frameworks = 'RKBluetoothLEKit/*.{framework}'

表示pod库项目依赖自己的framework

推送SDK项目到GitHub仓库

pod lib create命令创建的项目已经自动使用git版本管理使用下面的命令将项目推送到GitHub仓库

git remote add origin https://github.com/yuanzj/RKBluetoothLEKit.git //连接远程仓库并建了一个名叫:origin的别名
git push -u origin master //将本地仓库的东西提交到地址是origin的地址,master分支下###发布SDK到CocoaPods

创建Spec描述文件RKBluetoothLEKit.podspec中指定的版本tag并推送到GitHub仓库

git tag -a 0.1.0 -m "first release"
git push origin --tags

发布SDK项目到CocoaPods

注册账号

在开始提交项目到CocoaPods之前,需要我们先搞一个账号:

pod trunk register jswxyzj@qq.com 'yuanzj' --description='My own computer'

执行上面命令后需要去上面填写的注册邮箱中进行验证,验证后可以使用pod trunk me来查看是否"注册"成功。

zhijianuandeMBP:RKBluetoothLEKit YuanZhiJian$ pod trunk me
  - Name:     jswxyzj
  - Email:    jswxyzj@qq.com
  - Since:    March 7th, 19:30
  - Pods:
    - RKBluetoothLEKit
  - Sessions:
    - March 7th, 19:30 -      July 14th, 01:18. IP: 49.80.216.112
    - May 7th, 00:32   - September 12th, 01:03. IP: 60.250.10.247
    Description: My office computer
    - May 7th, 07:16   - September 13th, 07:17. IP: 203.69.59.198
    Description: My own computer

OK,接下来就可以发布SDK到CocoaPods了

验证.podspec文件

首先需要验证你的代码是否有错以及Podspec文件,那么这个时候下面这条命令就起到非常大的作用了:

pod lib lint RKBluetoothLEKit.podspec

这条命令是用来验证你的Podspec文件是否有问题,并且代码里面是否有问题命令。建议这条命令后面跟着--verbose的参数。 --allow-warnings 是否允许警告,在用到第三方框架的时候,有的时候是自带会有warmings的代码,用这参数可以屏蔽警告。这个参数强烈希望大家加上。

上传

终于到达上传的时间了,运行命令:

pod trunk push RKBluetoothLEKit.podspec  

遇到第三方库的告警导致终止添加--allow-warnings即可如:

 pod trunk push RKBluetoothLEKit.podspec --verbose  --allow-warnings

只要通过上述命令即可上传项目到Cocoapods官方的仓库里去,当然如果你有新的tag版本的话也是通过该命令进行上传新的版本到Cocoapods的。而我们可以通过pod search 名字来查询是否上传成功。

-> RKBluetoothLEKit (0.1.0)
   车精灵中控蓝牙SDK
   pod 'RKBluetoothLEKit', '~> 0.1.0'
Homepage: https://github.com/yuanzj/RKBluetoothLEKit
Source:   https://github.com/yuanzj/RKBluetoothLEKit.git
Versions: 0.1.0 [master repo]

尾巴

最后在集成了RKBluetoothLEKit库使用的时候发现能够编译和运行通过就是在代码中导入头文件的地方会提示 “could not build module ‘XXX’”
一开始以为RKBluetoothLEKit.podspec 文件编写有问题google 了很多地方尝试了很多方法结果都没有效果。

这个问题最近终于彻底完美解决了 把target 下 Build Settings 中 Allow Non-modular includes in Framework Modules 选项卡设为yes

在实施过程了查阅了很多资料在这里表示由衷的感谢

How to Use a Third Party Framework in a Private CocoaPod

Cocoapods系列教程(三)——私有库管理和模块化管理

使用CocoaPods开发并打包静态库