iOS DeviOS模块化CocoaPods framework

使用CocoaPod打包

2017-03-28  本文已影响1586人  brownfeng

前段时间将两个sdk都转向了cocoapod进行开发和打包.

1 基于pod命令创建SDK

具体的pod命令如下:

pod lib create MyCustomLib

调用以后terminal中会需要填写以下问题,一般选择创建Demo,其他选项根据需求填写:

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 ]
 > Specta

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

What is your class prefix?
 > BF

2 默认podspec文件的内容结构

podspec内容是ruby.用sublime设置成ruby可以看到语法高亮.

#
# Be sure to run `pod lib lint MyCustomLib.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'MyCustomLib'
  s.version          = '0.1.0'
  s.summary          = 'A short description of MyCustomLib.'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/brownfeng/MyCustomLib'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'brownfeng' => 'brownfeng@webank.com' }
  #s.source           = { :git => 'https://github.com/brownfeng/MyCustomLib.git', :tag => s.version.to_s }
  s.source           = { :git => '/Users/pp/Desktop/MyCustomLib'}
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

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

  # s.public_header_files = 'MyCustomLib/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
  
  #s.subspec 'vendorlibs' do |s|
    #s.ios.vendored_libraries = "MyCustomLib/YoutuLibs/*.a" #依赖了的.a文件
    #s.ios.vendored_frameworks = "MyCustomLib/YoutuLibs/*.framework"
  #end
end

podspec中重要的几个内容解释如下:

3 提交pod到私有pod库

一般我们提交源码到公有或者私有pod库时候,首先需要修改s.source,修改成git路径:

s.source = { :git => 'https://github.com/brownfeng/MyCustomLib.git', :tag => s.version.to_s }

s.source = { :git => 'https://github.com/brownfeng/MyCustomLib.git', :tag => '0.1.0' }

然后根据实际情况修改s.version,然后git的要提交的commit打上tag(tag与s.version的版本一致).

注意pod install或者pod package都是通过git的commit索引或者tag来找源码的.因此对外发布时候一定要在s.version中指定tag的标签,在git中给定的commit打上tag.

4 验证pod库是否满足pod要求

pod lib lint BZLib.podspec --allow-warnings --verbose

5 将sdk打包

需要安装cocoapods-packager.

然后执行pod package MyCustomLib.podspec --force --verbose.通过这个命令打包时候会自动将podspec中dependency的第三方库进行重命名.这样打出来的是MyCustomLib.framework.前面资源文件都在MyCustomLib.framework/Reources/MyCustomLib.bundle里面可以手动把这个bundle拖出去.

前面提到过使用pod package打包时候需要注意.pod会根据当前podspec中的s.source的地址去查找当前sdk源文件的地址.如果使用的s.source = { :git => 'https://github.com/brownfeng/MyCustomLib.git', :tag => s.version.to_s },那么就会去git中拉取最后一个tag对应commit的源码.如果使用的s.source = { :git => '/Users/pp/Desktop/MyCustomLib'},那么会使用当前sdk中git 的HEAD位置的commit的源码进行打包编译.(如果源码有修改,一定要先git commit,然后再打包)

6 注意问题

1 重点问题一: Example中依赖某些类库

举例说明,如果MyCustomLib.podspec中依赖s.dependency 'AFNetworking', '~> 2.3',此时在Example中也需要使用AFNetworking,那么这里千万不要在Example中引入AFNetworking的源码,请在podfile中添加pod AFNetworking.

2 重点问题二: SDK中依赖的第三方库无法使用BITCODE

在Example的podfile底部添加以下语句:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['ENABLE_BITCODE'] = 'NO'
        end
    end
end

3 重点问题三: SDK中调用资源的问题

由于最后打包时候有可能打出来的包是动态库,那么建议通过如下方式调用资源

// Load the framework bundle.
+ (NSBundle *)frameworkBundle {
  static NSBundle* frameworkBundle = nil;
  static dispatch_once_t predicate;
  dispatch_once(&predicate, ^{
    NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath];
    NSString* frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"Serenity.bundle"];
    frameworkBundle = [[NSBundle bundleWithPath:frameworkBundlePath] retain];
  });
  return frameworkBundle;
}

[UIImage imageWithContentsOfFile:[[[self class] frameworkBundle] pathForResource:@"image" ofType:@"png"]];

4 podfile的依赖路径

source 'https://github.com/CocoaPods/Specs.git'

use_frameworks!
install! 'cocoapods', :deterministic_uuids => false

target 'MyCustomLibExapmle' do
  pod 'MyCustomLib', :path => '../'   #使用的就是本地的podspec文件
  pod 'SDWebImage', '~> 1.0'
  target 'MyCustomLib.podspec_Tests' do
    inherit! :search_paths

    pod 'Specta'
    pod 'Expecta'
    pod 'FBSnapshotTestCase'
    pod 'Expecta+Snapshots'
  end
end

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['ENABLE_BITCODE'] = 'NO'
        end
    end
end

对于podfile,常见的库的地址引用写法如下:

pod '库名', :podspec => 'podspec文件路径'  #指定导入库的podspec文件路径

pod '库名', :git => '源码git地址'  #指定导入库的源码git地址

pod '库名', :tag => 'tag名'  #指定导入库的Tag分支

pod '库名', :path => '~/Documents/AFNetworking'  #指定本地的某个库,文件夹中要有podspec文件

AFNetworking举例:

// 使用仓库中的master分支:
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git'

//使用仓库的其他分支:
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :branch => 'dev'

//使用仓库的某个tag:
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0'

//指定一个提交记录:
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit => '082f8319af'

//指定本地路径的最近一次提交commit
pod '库名', :path => '~/Documents/AFNetworking'

5 私有库的依赖问题

公有库的依赖问题很方便,这里就不解释了.

如果自己的某个sdk的podspec文件中的dependency并未上传到公有或者私有pod库中.紧紧是使用的本地的podspec库.那么此时不能使用pod package打包成功.但是可以通过编写podfile方式,在Example中集成sdk.具体在podfile中需要手动写入需要引入的podspec的地址,而且两者有引入的顺序要求.

举例说明, 如果我们有一个主工程Downloader,它使用一个子库Player,这个Player子库依赖另外一个库FFMpegPlayer.在Player的podspec中的依赖只能写成s.dependency = 'FFMpegPlayer'(虽然有可能FFMpegPlayer库并没有提交到公有或者私有pod库中,这种写法是满足的).同时在我们的Example Downloaderpodfile中需要按照顺序写入引入的库:

pod 'FFMpegPlayer', :path => '../FFMpegPlayer' (micro project)
pod 'Player', :path => '../Player' (small project which depends on FFMpegPlayer)

一定要注意顺序!!!!!

注意,这里如果要使用pod package打包Player库会失败.

参考文档

http://www.jianshu.com/p/8af475c4f717

http://stackoverflow.com/questions/16905112/cocoapods-dependency-in-pod-spec-not-working

http://feihu.me/blog/2016/unittest-for-framework-in-big-project/

上一篇下一篇

猜你喜欢

热点阅读