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           = { '赵桂安' => '[email protected]' }
    #项目地址,这里不建议使用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/'
    #最低支持的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 % 

完成

你可能感兴趣的:(iOS创建CocoaPods私有库)