iOS开发经验 —— 封装私有pod

字数 991阅读 1809

写在前面

说明:
两个私有库:
一个是私有代码库,存放需要pod封装的代码
一个是私有索引库,模拟cocoapods公共索引的库,通过这个索引找到私有代码库

步骤

1,根据命令提示创建工程

$ pod lib create CocoapodDemo

根据提问、提示创建一个demo工程


image

image

CocoapodDemo/CocoapodDemo/Classes中存放的就是Pod要包装的files

2,上传源码

把这个工程里的代码上传到远程私有代码库(没有的话新建一个)

3,创建索引库

新建一个远程仓库用于存放自己的私有库的 .podspec文件(私有索引库), 例如 git@github.com:yongqianvip/privatePodspec.git

通过下面的命令创建本地私有pod索引库

$ pod repo add privatePodspec git@github.com:yongqianvip/privatePodspec.git 

如果不行换用https://开头的链接 ,然后再看 .cocoapods/repos/ 文件夹下就多了一个privatePodspec文件夹,里面的master文件夹是pod公共索引库,包含成千上万的pod索引,privatePodspec就是模拟这个公共索引库的私有索引库

image

4,功能实现

到这里,私有代码库和私有索引库都创建完毕,接下来就是功能实现,想要实现什么功能,把相应的功能实现文件、头文件等放入CocoapodDemo/CocoapodDemo/Classes目录下,在Example文件夹下的工程中进行测试,然后一并上传到远程代码库

image

5,编辑CocoapodDemo.podspec文件

这个应该无需过多解释,主要配置好sourcesource_files,更多参数送传送门http://guides.cocoapods.org/syntax/podspec.html

image

6,验证CocoapodDemo.podspec文件

$ pod lib lint

在CocoapodDemo.podspec文件所在目录下执行上面的命令,如果验证通过会显示

image

如果有ERROR一定要先修改错误,如果是可以忽略的警告加 --allow-warnings 参数

文末列出了几个错误,可以参考,也欢迎补充。。。

7,打tag提交代码

podspec验证通过之后,私有代码库打tag提交

$ git tag '0.0.1'
$ git push --tags

tag 要和 .podspec文件中的version一致

8,添加到本地索引库

$ pod repo push privatePodspec CocoapodDemo.podspec

注意: 如果lint的时候添加了 --allow-warnings --use-libraries 等参数的话,push的时候也要加上

image

9,验证

$ pod search RRCLogin

image

如果之前步骤都成功,但是验证环节报错
[!] Unable to find a pod with name, author, summary, or description matching 'RRCLogin'
可能是缓存的问题,清理cocoapodsearch缓存

$ rm ~/Library/Caches/CocoaPods/search_index.json

再执行search(清理search的缓存之后再搜索时间会有点儿长)

10,使用我们上传的私有pod

在需要引用这个库的工程的Podfile中添加

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

这是为了告诉pod,检索依赖的时候既要检索公共索引库,咱们本地的私有索引库也要检索

总结

这里是借助公开库 模拟私有库的pod封装,我们的索引没有推送到cocoapod远程库,假想一下,如果我们的这两个仓库都是私有的,那么别人就搜不到我们的代码,就实现了真正的私有pod

pod lib lint 时遇到的一些ERROR

错误 NO.1

  Building with xcodebuild. 
xcodebuild clean build -workspace /var/folders/nc/2btst88j1dn9spsydgzy94m80000gp/T/CocoaPods-Lint-20180301-68829-1lc144r-RRCLogin-Inhouse/App.xcworkspace -scheme App -configuration Release CODE_SIGN_IDENTITY=- -sdk iphonesimulator -destination id=EF5C4592-DA0A-40C4-B14A-040C9934FA86 
Build settings from command line: 
    CODE_SIGN_IDENTITY = - 
    SDKROOT = iphonesimulator11.2 

xcodebuild: error: Unable to find a destination matching the provided destination specifier: 
        { id:EF5C4592-DA0A-40C4-B14A-040C9934FA86 } 

    Available destinations for the "App" scheme: 
        { platform:iOS, id:3364f575ca6f4054e91f7273bbf1dfcd057a4786, name:YYQ的 iPhone } 
        { platform:iOS Simulator, id:B461000D-7668-4984-969E-F4A6F2B5F822, OS:11.0.1, name:iPad (5th generation) } 
        { platform:iOS Simulator, id:274C9F62-9F24-4A68-97FA-F58ACBD752D8, OS:11.2, name:iPad (5th generation) } 
        { platform:iOS Simulator, id:A5594DF0-938B-4AD3-A2E4-D9DF0C462206, OS:11.2, name:iPhone 5s } 
        { platform:iOS Simulator, id:0443319F-6EF8-49CF-A93F-0EACA18B1BAB, OS:11.2, name:iPhone 6 } 
        { platform:iOS Simulator, id:6B8AB42A-32BB-476C-B93A-796D0A5F0D1B, OS:11.2, name:iPhone 6 Plus } 
        { platform:iOS Simulator, id:25F0DBDA-4F38-4004-AA01-8250302B466B, OS:11.2, name:iPhone 6s } 
        { platform:iOS Simulator, id:177042D1-C48E-4F0E-A285-F155B7D2A608, OS:11.2, name:iPhone 6s Plus } 
        { platform:iOS Simulator, id:FA65D37F-74A6-40C9-817E-9C929D660348, OS:11.2, name:iPhone 7 } 
        { platform:iOS Simulator, id:2DD23829-DC3C-49B5-822E-D859B0B03E5C, OS:11.2, name:iPhone 7 Plus } 
        { platform:iOS Simulator, id:A24F0CBF-55B5-46B2-9B4A-36FFD6C4E7D1, OS:11.2, name:iPhone 8 } 
        { platform:iOS Simulator, id:E84B2668-BC88-42FA-8A4E-FF731AA52383, OS:11.2, name:iPhone 8 Plus } 
        { platform:iOS Simulator, id:770C7323-027A-45EF-87E4-C07DECA5B116, OS:11.2, name:iPhone SE } 
        { platform:iOS Simulator, id:1368D6CC-FE26-4403-BD4F-B6FDD6AFE47F, OS:11.2, name:iPhone X } 

    Ineligible destinations for the "App" scheme: 
        { platform:iOS, id:dvtdevice-DVTiPhonePlaceholder-iphoneos:placeholder, name:Generic iOS Device } 
        { platform:iOS Simulator, id:dvtdevice-DVTiOSDeviceSimulatorPlaceholder-iphonesimulator:placeholder, name:Generic iOS Simulator Device } 
   Testing with xcodebuild. 
-> RRCLogin-Inhouse (0.1.0) 
    - WARN  | url: There was a problem validating the URL http://gitlab.renrenche.com/DQD_IOS_module/RRCInhouseLogin. 
    - ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. 
    - NOTE  | [iOS] xcodebuild:  xcodebuild: error: Unable to find a destination matching the provided destination specifier: 

[!] RRCLogin-Inhouse did not pass validation, due to 1 error and 1 warning. 
You can use the `--no-clean` option to inspect any issue. 

这个错误目前还不清楚原因,初步推测是环境变量的问题,为了解决这个错误,我换了台电脑、我换了台电脑、我换了台电脑

错误 NO.2

- ERROR | xcodebuild:  /Users/YYQ/Library/Developer/Xcode/DerivedData/App-aajotsmzgxneapbmwyfqevaamvpu/Build/Products/Release-iphonesimulator/RRCLogin-Inhouse/RRCLogin_Inhouse.framework/Headers/RRCPayNetworkHttpResponse.h:10:9: error: include of non-modular header inside framework module 'RRCLogin_Inhouse.RRCPayNetworkHttpResponse': '/Users/YYQ/Library/Developer/Xcode/DerivedData/App-aajotsmzgxneapbmwyfqevaamvpu/Build/Products/Release-iphonesimulator/JSONModel/JSONModel.framework/Headers/JSONModel.h' [-Werror,-Wnon-modular-include-in-framework-module] 

这个错误是文件引用的问题,我的代码中用到了JSONModel,引用的方式是 #import "JSONModel.h”,但是JSONModel是通过s.dependency 'JSONModel’, ‘~> 1.5.1’的方式,引入的,那么我自己的代码中使用JSONModel就应该是用尖括号的形式引入了
#import <JSONModel.h>这是不对的,应该用#import <JSONModel/JSONModel.h>,否则会有下边的错
https://github.com/CocoaPods/CocoaPods/issues/5715

错误 NO.3

- ERROR | [iOS] xcodebuild:  /Users/YYQ/RRC/Podmodules/RRCLogin-Inhouse/RRCLogin-Inhouse/Classes/login/GETVerificationVoiceCodeRequest.m:11:9: error: 'JSONModel.h' file not found with <angled> include; use "quotes” instead 

这个错误是路径的问题,我的代码中用到了JSONModel,引用的方式是 #import <JSONModel.h>,改成#import <JSONModel/JSONModel.h> 就可以了

错误 NO.4

- ERROR | [iOS] unknown: Encountered an unknown error (The 'Pods-App' target has transitive dependencies that include static binaries: (/private/var/folders/m7/7t1r7_993fbgs40fppwxrsm80000gn/T/CocoaPods-Lint-20180301-60100-1meifl-Payment/Pods/openssl-ios-bitcode/lib/libcrypto.a and /private/var/folders/m7/7t1r7_993fbgs40fppwxrsm80000gn/T/CocoaPods-Lint-20180301-60100-1meifl-Payment/Pods/openssl-ios-bitcode/lib/libssl.a)) during validation. 

依赖了其他库,lint 检查的时候添加后缀

$ pod lib lint --use-libraries

推荐阅读更多精彩内容