iOS集合OC基础

Cocoapods生成静态库(完整)

2018-01-23  本文已影响1246人  程序员不务正业

1、Cocoapods环境安装

cocoapods-packager打包工具安装
sudo gem install cocoapods-packager

2、终端cd 到目录创建SDK

pod lib create iComeSKD

并根据提示完成创建demo工程

3、工程中完善iComeSKD.podspec文件

#
# Be sure to run `pod lib lint iComeSDK.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             = 'iComeSDK'
s.version          = '0.1.3'
s.summary          = 'A short description of iComeSDK.'

# 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      = "智慧平台为iCome项目提供的IM专用SDK文件"

s.homepage         = 'https://github.com/zhangrongwu/iComeSDK'
# s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license          = 'MIT'
s.author           = { 'zhangrongwu' => 'zhangrongwuios@sina.com'}
#s.source           = { :git => '/Users/zhangrongwu/iComeProj/iComeSDK', :tag => s.version.to_s }
s.source           = { :git => 'https://source.enncloud.cn/zhangrongwu/iComeSDK', :tag => s.version.to_s }

# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

s.ios.deployment_target = '8.0'

#相对路径很重要,不正确无法引入SDK
s.source_files  = 'iComeSDK/*.{h,m}'
s.source_files  = 'iComeSDK'
s.source_files  = 'iComeSDK/**/*.{h,m}'

s.public_header_files = 'iComeSDK/Header/*.h'

s.resources  = 'iComeSDK/*.{bundle,png,plist}'
#s.resources = 'iComeSDK/**/*.png'

#s.resource_bundles = {
#'iComeSDK' => ['iComeSDK/*.{bundle,png,plist}']
#}

s.frameworks = 'UIKit'
s.dependency 'AFNetworking', '~> 3.1.0'
s.dependency 'FMDB', '~> 2.6.2'
s.dependency 'MBProgressHUD',    '~> 1.0.0'
s.dependency 'SDWebImage',   '~> 3.7.6'
s.dependency 'CocoaAsyncSocket', '~> 7.4.3'
s.dependency 'SAMKeychain',   '~> 1.5.3'
s.dependency 'MJExtension',  '~> 3.0.10'
s.dependency 'MLLabel',  '~> 1.9.1'
s.dependency 'Masonry',  '~> 1.1.0'
s.dependency 'YYKit',  '~> 1.0.9'
s.dependency 'MJRefresh',    '~> 3.1.14.1'
s.dependency 'TZImagePickerController',  '1.7.9'
s.dependency 'FDFullscreenPopGesture',   '~>1.1'
end

4、使用自己的文件替换Pods下ReplaceMe.m文件并完善

完成后在cd 到 Example 执行

pod install 

在demo工程中完善你的sdk使用方法

5、打包具体步骤,顺序很重要

5.1、开发完成静态类库之后,需要运行pod lib lint验证一下类库是否符合pod的要求(cd到podspec文件所在目录下)

pod lib lint iComeSDK.podspec  --no-clean --verbose --allow-warnings 

5.2、更新pod(cd到Podfile文件所在目录下)

pod update --verbose --no-repo-update 或者pod install

5.3、提交,更新版本号(cd到podspec文件所在目录下)

git add .
git commit -a -m'v你新的版本号'
git tag -a 你新的版本号 -m'v你新的版本号'
git push origin 版本号
git push -u origin master 

5.4、打包(cd到podspec所在文件目录下)
打包前修改iComeSDK.podspec中的版本号,修改为5.3时你提交的版本号

pod package iComeSDK.podspec --library --force     打包成.a文件。--force是指强制覆盖

pod package iComeSDK.podspec --force               打包成.framework文件

添加为私有库方式

################################私有库##########################################
privateSpecs='存放spec文件的私有库地址'
cocoapodsSpecs='https://github.com/CocoaPods/Specs'
#私有库校验
pod spec lint --sources='${privateSpecs},${cocoapodsSpecs}' --no-clean --private --allow-warnings --verbose
#发布私有pod
pod repo push ${frameworkName}.podspec --sources='${privateSpecs},${cocoapodsSpecs}' --verbose --allow-warnings
##########################################################################

pod repo add REPO_NAME SOURCE_URL // 添加spec到服务器

其中REPO_NAME为你存放私有.podspec文件的目录

例如:在你的私有git上创建一个仓库,并执行以下代码提交到你的私有仓库
pod repo add iComeSpecs http://git.oschina.net/xxx/iComeSpecs.git

坑(路径很重要)

1、pod下文件路径问题

s.source_files和s.public_header_files关系,s.source_files中必须包含s.public_header_files文件,不然pod install后会少一些文件

2、s.resources文件问题,加载的bundle方式

+ (UIImage *)imageNamedFromiComeSDKBundle:(NSString *)name {
    
    NSBundle *imageBundle = [self ic_imagePickerBundle];
    name = [name stringByAppendingString:@"@2x"];
    NSString *imagePath = [imageBundle pathForResource:name ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    if (!image) {
        // 兼容业务方自己设置图片的方式
        name = [name stringByReplacingOccurrencesOfString:@"@2x" withString:@"@3x"];
        NSString *imagePath = [imageBundle pathForResource:name ofType:@"png"];
        image = [UIImage imageWithContentsOfFile:imagePath];
    }
    return image;
}

+ (NSBundle *)ic_imagePickerBundle {
    NSBundle *bundle = [NSBundle bundleForClass:[iComeApiManager class]];
    NSURL *url = [bundle URLForResource:@"iComeBundle" withExtension:@"bundle"];
    bundle = [NSBundle bundleWithURL:url];
    return bundle;
}

3、加载Bundle时可能会碰到not yet loaded,不知道为啥,我这直接忽略了,然后图片正常加载

    NSBundle *imageBundle = [self ic_imagePickerBundle];

4、Mac的存储空间重要,额,自己的时256的存储,导致打包失败。清理回收站后成功
5、5.3和5.4认真读,打包前修改*.podspec文件,不是把修改后的 iComeSDK.podspec提交上去。
6、关于资源bundle文件在静态库中存在,然后使用静态库时未能找到问题,处理方案:把bundle资源文件放到静态库同层级目录即可


83B9C0DD-AD5B-443A-A026-5F4D491BF27E.png

上方两个为资源文件。不明白为啥不能直接放到.framework中。
知道的告诉我

target has transitive dependencies that include static binaries:

处理方案:
1、删除 use_frameworks!
2、pod install

上一篇下一篇

猜你喜欢

热点阅读