美文网首页CocoaPods
iOS 创建CocoaPods私有库

iOS 创建CocoaPods私有库

作者: jsone | 来源:发表于2020-03-19 18:23 被阅读0次
    CocoaPods

    一、简介

    为了实现组件化开发,方便每个模块到开发和版本控制,为每个组件创建CocoaPods私有库是目前最有效的方案。
    最新最详细的教程请前往iOS CocoaPods私有库的创建和版本更新

    二、步骤

    1. 打开终端cd进入到要创建私有库到目录
    2. 在终端上执行pod lib create <私有库名称>来创建库
    OneMacBookPro:YDShelfLife admin$ cd /Users/admin/Documents/RemoteLibrary
    OneMacBookPro:RemoteLibrary admin$ pod lib create PrivatePod
    Cloning `https://github.com/CocoaPods/pod-template.git` into `PrivatePod`.
    Configuring PrivatePod template.
    security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
    
    ------------------------------
    
    To get you started we need to ask a few questions, this should only take a minute.
    
    2020-03-19 11:23:32.387 defaults[62448:8968663]
    The domain/default pair of (org.cocoapods.pod-template, HasRunBefore) does not exist
    If this is your first time we recommend running through with the guide:
    - https://guides.cocoapods.org/making/using-pod-lib-create.html
    ( hold cmd and click links to open in a browser. )
    
    Press return to continue.
    

    创建前有几个配置需要进行选择

    # 选择使用的平台
    What platform do you want to use?? [ iOS / macOS ]
     > iOS
    # 选择使用的语言
    What language do you want to use?? [ Swift / ObjC ]
     > ObjC
    # 库是否包含demo应用
    Would you like to include a demo application with your library? [ Yes / No ]
     > Yes
    # 选择用来测试的framework
    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?
     > YD
    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 `PrivatePod` from `../`
    Downloading dependencies
    Installing FBSnapshotTestCase (2.1.4)
    Installing PrivatePod (0.1.0)
    Generating Pods project
    Integrating client project
    
    [!] Please close any current Xcode sessions and use `PrivatePod.xcworkspace` for this project from now on.
    Sending stats
    Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.
    
     Ace! you're ready to go!
     We will start you off by opening your project in Xcode
      open 'PrivatePod/Example/PrivatePod.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`.
    
    1. cd到测试项目Example目录,执行pod install命令,完成
    OneMacBookPro:RemoteLibrary admin$ cd /Users/admin/Documents/RemoteLibrary/PrivatePod/Example
    OneMacBookPro:Example admin$ pod install
    Analyzing dependencies
    Fetching podspec for `PrivatePod` from `../`
    Downloading dependencies
    Using FBSnapshotTestCase (2.1.4)
    Using PrivatePod (0.1.0)
    Generating Pods project
    Integrating client project
    Sending stats
    Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.
    
    1. 打开Example项目的PrivatePod.xcworkspace文件,将源文件目录Classes拖进项目,并新建类目NSString+YDCategory文件
      源文件目录Classe

    文件目录'Classes'拖进项目的目的是可以自己在源文件进行修改,也可以自己在项目文件夹下新新建个目录,然后再修改一下PrivatePod.podspec文件中的源文件路径s.source_files,'Classes'目录中.gitkeep文件可能被一起拖进项目,可以删除.gitkeep文件的引用。

    新建类目NSString+YDCategory文件

    在类目中添加方法printSelf

    - (void) printSelf
    {
        NSLog(@"YDCategory: %@", self);
    }
    

    在控制器中添加代码

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.title = @"PrivatePod";
        [self.title printSelf];
    }
    

    运行项目输出结果:

    运行结果
    1. 修改PrivatePod.podspec文件
      使用Sublime Text打开
      PrivatePod.podspec文件
    Pod::Spec.new do |s|
      s.name             = 'PrivatePod'
      s.version          = '0.1.0'
      s.summary          = 'Test Private Podspec.'
    
    # 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://www.jianshu.com/u/6b426bd2ebfd'
      # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
      s.license          = { :type => 'MIT', :file => 'LICENSE' }
      s.author           = { '作者' => '邮箱' }
      s.source           = { :git => 'git地址', :tag => s.version.to_s }
      # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
    
      s.ios.deployment_target = '8.0'
    
      s.source_files = 'PrivatePod/Classes/**/*'
      
      # s.resource_bundles = {
      #   'PrivatePod' => ['PrivatePod/Assets/*.png']
      # }
    
      # s.public_header_files = 'Pod/Classes/**/*.h'
      # s.frameworks = 'UIKit', 'MapKit'
      # s.dependency 'AFNetworking', '~> 2.3'
    end
    

    编辑完成后保存一下,Example项目重新pod install就可以看到Pod中的源文件有刚新建的类目文件

    Pod中的源文件
    1. 在码云上新建对应的私有库PrivatePod


      新建私有库

    将生成的git地址在PrivatePod.podspec文件中替换对应的git地址

    s.source           = { :git => 'git地址', :tag => s.version.to_s }
    
    1. 验证Pod配置文件pod lib lint
      验证出错
      验证失败,原因是CocoaPods版本太低
      更新CocoaPods
    sudo gem install cocoapods
    

    再次验证通过


    验证通过
    1. 将项目推送到远程Git仓库
    OneMacBookPro:PrivatePod admin$ git remote add origin https://gitee.com/xxxxxx/PrivatePod.git
    # 添加所有文件到缓存
    OneMacBookPro:PrivatePod admin$ git add .
    # 将缓存内容提交到本地仓库
    OneMacBookPro:PrivatePod admin$ git commit -a -m "首次提交 version 0.1.0"
    # 将本地仓库推送到远程仓库
    OneMacBookPro:PrivatePod admin$ git pull origin master --allow-unrelated-histories
    

    推送到远程仓库时出现了冲突,导致推送失败

    warning: no common commits
    remote: Enumerating objects: 4, done.
    remote: Counting objects: 100% (4/4), done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 4 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (4/4), done.
    From https://gitee.com/heroyoungday/PrivatePod
     * branch            master     -> FETCH_HEAD
     * [new branch]      master     -> origin/master
    Auto-merging README.md
    CONFLICT (add/add): Merge conflict in README.md
    Automatic merge failed; fix conflicts and then commit the result.
    OneMacBookPro:PrivatePod admin$ git push origin master
    To https://gitee.com/heroyoungday/PrivatePod.git
     ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to 'https://gitee.com/heroyoungday/PrivatePod.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    冲突出现在README.md文件,因为前面新建仓库的时候,错误的勾选了使用Readme文件初始化这个仓库,本地创建CocoaPods私有库时也自动生成了一个Readme文件,导致推送时发生冲突。

    不要添加readme文件

    我使用了sourcetree的代码合并工具,删除了远程仓库的readme文件内容,推送成功了,并打了标签

    使用sourceTree推送成功
    1. 创建本地Spec管理库
    OneMacBookPro:Desktop admin$ cd /Users/admin/Documents/RemoteLibrary/PrivatePod
    OneMacBookPro:PrivatePod admin$ pod repo add PrivatePod https://gitee.com/heroyoungday/PrivatePod.git
    Cloning spec repo `PrivatePod` from `https://gitee.com/heroyoungday/PrivatePod.git`
    OneMacBookPro:PrivatePod admin$ pod repo push PrivatePod PrivatePod.podspec
    
    Validating spec
     -> PrivatePod (0.1.0)
        - NOTE  | xcodebuild:  note: Using new build system
        - 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 'PrivatePod' 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 `PrivatePod' repo
    
    
    Adding the spec to the `PrivatePod' repo
    
     - [Update] PrivatePod (0.1.0)
    
    Pushing the `PrivatePod' repo
    
    1. 验证私有库是否发布成功
      新建一个项目PrivatePodDemo,cd进入项目根目录,执行vim Podfile命令,Podfile的内容为
    platform :ios,'8.0'
    target 'PrivatePodDemo' do
       pod 'PrivatePod',:git => 'https://gitee.com/xxxxxxxx/PrivatePod.git'
    end
    

    查看Pod中的源码


    查看Pod中的源码

    在ViewController文件中添加代码

    #import "ViewController.h"
    #import "NSString+YDCategory.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"PrivatePodDemo";
        [self.title printSelf];
    }
    @end
    

    运行结果:


    运行结果

    参考文章:
    IOS创建CocoaPods私有库
    iOS创建自己的私有cocoaPods库

    相关文章

      网友评论

        本文标题:iOS 创建CocoaPods私有库

        本文链接:https://www.haomeiwen.com/subject/ypbkyhtx.html