iOS创建CocoaPods私有库

前言

iOS开发者,会经常使用CocoaPods管理第三方库。但是,无论是组件化开发还是需要创建公司内部的类库,我们都可以制作自己私有的CocoaPods库。本文就是详细说明私有库创建流程。

1.准备工作

  • 创建两个私有远程仓库 LWSpec 和 LWLib
    • WYSpec仓库用来存储本地spec
    • WYLib用来存储项目工程文件

2.创建pod私有库的项目工程

  • cd到合适目录下用命令行创建工程

  • 执行pod lib create WYLib

    MacBook-Pro ~ % cd desktop
    MacBook-Pro desktop % pod lib create LWLib
    
  • 回车之后终端会进行灵魂,按照自己的需求回答。

    #选择平台
    What platform do you want to use?? [ iOS / macOS ]
     > iOS
    #选择编程语言
    What language do you want to use?? [ Swift / ObjC ]
     > Objc
    #在你的项目中是否创建一个demo工程,为了方便测试,我选择了Yes
    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 ]
     > Yes
    # 类前缀名
    What is your class prefix?
     > LW
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    
    Running pod install on your new library.
    
    Analyzing dependencies
    Fetching podspec for `YOURPODNAME` from `../`
    Downloading dependencies
    Installing YOURPODNAME (0.1.0)
    Generating Pods project
    Integrating client project
    
    [!] Please close any current Xcode sessions and use `LWLib.xcworkspace` for this project from now on.
    Sending stats
    Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
    
    Ace! you're ready to go!
    We will start you off by opening your project in Xcode
    open 'LWLib/Example/LWLib.xcworkspace'
    
    To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
    To learn more about creating a new pod, see   `https://guides.cocoapods.org/making/making-a-cocoapod`.
    
  • 创建完成后自动打开

  • 然后进入LWLib文件夹中找到Classes中的ReplaceMe.m文件删除,然后将自己所需要的文件放到这里目录下

  • cdExample路径下,执行pod install更新Example中的pod

  • 打开Example中的.workspace文件,打开工程。

    找到. podspec

  • 修改. podspec

    Pod::Spec.new do |s|
    #项目名称
    s.name             = 'LWLib'
    #版本号
    s.version          = '0.1.0'
    #项目摘要
    s.summary          = 'LWLib.'
    # 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
    LWLib的项目描述,这是自己创建的一个项目
                         DESC
                         
    #主页,这里要填写可以访问的地址,不然验证不通过
    #个人远程仓库的地址
    s.homepage         = 'https://gitee.com/ZhaoLaoWu/LWLib'
    # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
    s.license          = { :type => 'MIT', :file => 'LICENSE' }
    #作者信息
    s.author           = { '赵桂安' => '623916490@qq.com' }
    #项目地址,这里不建议使用ssh地址,会有警告;建议使用HTTP或者HTTPS,最好使用HTTPS。
    #更改成自己的远程仓库地址
    s.source           = { :git => 'https://gitee.com/ZhaoLaoWu/LWLib.git', :tag => s.version.to_s }
    # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
    #最低支持的iOS版本
    s.ios.deployment_target = '8.0'
    #代码源文件地址
    s.source_files = 'LWLib/Classes/**/*'
    
    # s.resource_bundles = {
    #   'LWLib' => ['LWLib/Assets/*.png']
    # }
    
    # s.public_header_files = 'Pod/Classes/**/*.h'
    
    #根据自己是否有依赖库来决定是否添加
    #依赖的frameworks
    # s.frameworks = 'UIKit', 'MapKit'
    #依赖的公共库或者私有库
    # s.dependency 'AFNetworking', '~> 2.3'
    end
    
    
  • 修改完成后,cdLWLib文件

    执行pod lib lint

    出一下表示成功

    MacBook-Pro LWLib % pod lib lint
    
     -> LWLib (0.1.0)
      #注意如果这有警告,最好把问题解决了,我这个是已经解决的。
      - NOTE  | xcodebuild:  note: Using new build system
      - NOTE  | xcodebuild:  note: Building targets in parallel
      - NOTE  | [iOS] xcodebuild:  note: Planning build
      - NOTE  | [iOS] xcodebuild:  note: Constructing build description
      - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'LWLib' from project 'Pods')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
    
      LWLib passed validation.
      #已经完成通过
    

3.将本地文件上传到远程仓库

MacBook-Pro LWLib % git remote add origin https://gitee.com/ZhaoLaoWu/LWLib.git
MacBook-Pro LWLib % git add .
MacBook-Pro LWLib % git commit -m "Initial commit"
MacBook-Pro LWLib % git push -u origin master 
#To https://gitee.com/ZhaoLaoWu/LWLib.git
# ! [rejected]        master -> master (fetch first)
#error: failed to push some refs to 'https://gitee.com/ZhaoLaoWu/LWLib.git'
#hint: Updates were rejected because the remote contains work that you do
#hint: not have locally. This is usually caused by another repository pushing
#hint: to the same ref. You may want to first integrate the remote changes
#hint: (e.g., 'git pull ...') before pushing again.
#hint: See the 'Note about fast-forwards' in 'git push --help' for details.
#注意:当执行推送远端失败时,先执行以下操作
MacBook-Pro LWLib % git pull origin master
#From https://gitee.com/ZhaoLaoWu/LWLib
# * branch            master     -> FETCH_HEAD
#fatal: refusing to merge unrelated histories
#注意:如果出现上面所示错误,需添加--allow-unrelated-histories即可
MacBook-Pro LWLib % git pull origin master --allow-unrelated-histories
#From https://gitee.com/ZhaoLaoWu/LWLib
# * branch            master     -> FETCH_HEAD
#CONFLICT (add/add): Merge conflict in README.md
#Auto-merging README.md
#Automatic merge failed; fix conflicts and then commit the result.
# 合并README.md的冲突 然后 commit
MacBook-Pro LWLib % git add .
MacBook-Pro LWLib % git commit -m '解决合并冲突' 
# 推送更新到远程仓库
#注意:第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,后续推送不需要再使用-u参数
MacBook-Pro LWLib % git push -u origin master
#Enumerating objects: 116, done.
#Counting objects: 100% (116/116), done.
#Delta compression using up to 4 threads
#Compressing objects: 100% (104/104), done.
#Writing objects: 100% (114/114), 53.17 KiB | 3.54 MiB/s, done.
#Total 114 (delta 25), reused 0 (delta 0)
#remote: Resolving deltas: 100% (25/25), completed with 1 local object.
#remote: Powered by GITEE.COM [GNK-5.0]
#To https://gitee.com/ZhaoLaoWu/LWLib.git
#   82f9eaf..b38a835  master -> master
#Branch 'master' set up to track remote branch 'master' from 'origin'.
#tag 值要和podspec中的version一致
MacBook-Pro LWLib % git tag 0.1.0
#推送tag到服务器上
MacBook-Pro LWLib % git push --tags
#Total 0 (delta 0), reused 0 (delta 0)
#remote: Powered by GITEE.COM [GNK-5.0]
#To https://gitee.com/ZhaoLaoWu/LWLib.git
# * [new tag]         0.1.0 -> 0.1.0

这是查看远程仓库已经有代码了

4.校验Spec

  • 执行pod spec lint

    zhaoguian@zhaoguiandeMacBook-Pro LWLib % pod spec lint
    
     -> LWLib (0.1.0)
      - NOTE  | xcodebuild:  note: Using new build system
      - NOTE  | xcodebuild:  note: Building targets in parallel
      - NOTE  | [iOS] xcodebuild:  note: Planning build
      - NOTE  | [iOS] xcodebuild:  note: Constructing build description
      - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'LWLib' from project 'Pods')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
      - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
    
      Analyzed 1 podspec.
    
      LWLib.podspec passed validation.
      
    

    出现上述描述表示成功。

5.创建spec repo

  • 进入文件夹~/.cocoapods/repos可以查看本地spec repo

  • 指定管理 lib repospecs repourl
    pod repo add LWLib https://gitee.com/ZhaoLaoWu/LWSpec.git
    注意 此处地址是spec仓库地址

  • 推送 podspecspecs repo
    pod repo push LWLib LWLib.podspec --sources=https://gitee.com/ZhaoLaoWu/LWSpec.git

    MacBook-Pro LWLib % pod repo push LWLib LWLib.podspec --sources=https://gitee.com/ZhaoLaoWu/LWSpec.git
    
    Validating spec
     -> LWLib (0.1.0)
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Building targets in parallel
        - NOTE  | [iOS] xcodebuild:  note: Planning build
        - NOTE  | [iOS] xcodebuild:  note: Constructing build description
        - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
        - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'LWLib' from project 'Pods')
        - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Pods-App' from project 'Pods')
        - NOTE  | [iOS] xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
    
    Updating the `LWLib' repo
    
    
    Adding the spec to the `LWLib' repo
    
    - [Add] LWLib (0.1.0)
    
    Pushing the `LWLib' repo
    

    出现上述表示已经成功

6.验证

  • 创建一个新项目,在Podfile中创建并使用
source ‘https://gitee.com/ZhaoLaoWu/LWSpec.git’
platform:ios, '10.0'
target 'CSLWLib' do
 use_frameworks!
 pod 'LWLib'
end
  • cd项目中 执行pod install
MacBook-Pro %  ~ % cd /Users/zhaoguian/Desktop/CSLWLib 
MacBook-Pro CSLWLib % pod install

Analyzing dependencies
Downloading dependencies
Installing LWLib (0.1.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

[!] Smart quotes were detected and ignored in your Podfile. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.
zhaoguian@localhost CSLWLib % 

完成

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345