美文网首页
利用Cocoapods制作公开库

利用Cocoapods制作公开库

作者: Frankkkkk | 来源:发表于2021-04-29 15:44 被阅读0次

    在本文开始之前,强烈建议先在CocoaPods官网上学习,仍然有疑问的,再来看本文的教程。

    本文采用Trunk方式来制作公开库。
    1、打开终端,在自己喜欢的目录下,执行pod lib create [pod name]。下图我创建的公开库的名称是YunxiaoLibrary.

    wenjun@WendeMacBook-Pro MyPublicLib % pod lib create YunxiaoLibrary
    Cloning `https://github.com/CocoaPods/pod-template.git` into `YunxiaoLibrary`.
    Configuring YunxiaoLibrary 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.
    
    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 double click links to open in a browser. )
    

    2、接着根据实际情况,回答几个问题

    To get you started we need to ask a few questions, this should only take a minute.
    
    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 double click links to open in a browser. )
    
    
    What platform do you want to use?? [ iOS / macOS ]
     > iOS
    
    What language do you want to use?? [ Swift / ObjC ]
     > ObjC
    
    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 ]
     > No
    
    What is your class prefix?
     > YX
    

    3、第2步结束后,会自定打开Xcode目录
    目录结构和各文件的说明,请参考using-pod-lib-create

    4、在Trunk上注册自己的账号(用邮箱注册)

    wenjun@WendeMacBook-Pro MyPublicLib % pod trunk register xxx@163.com '你的用户名' --description='macbook pro' 
    

    注册后,打开上述邮箱中的邮件,点击激活链接去激活

    5、pod trunk me查看注册信息

    wenjun@WendeMacBook-Pro MyPublicLib % pod trunk me
      - Name:     XXX
      - Email:    xxx@163.com
      - Since:    August 24th, 2018 03:49
      - Pods:
        - XXX
      - Sessions:
        - April 28th, 20:00 - September 3rd, 20:03. IP: 18.162.242.196
        Description: macbook pro
    

    因为我的邮箱之前注册过,所以Pods下有一个库了。

    6、在Github上新建一个公开仓库,用来存放该公开库
    因为后面要修改[NAME.podspec]文件,该文件中的要指定公开库地址,所以在这一步把仓库建好。

    新建公开仓库

    7、将本地公开库,推送到刚才新建的远程仓库

    wenjun@WendeMacBook-Pro MyPublicLib % cd /Users/wenjun/Workspace/MyPublicLib/YunxiaoLibrary
    wenjun@WendeMacBook-Pro YunxiaoLibrary % ls
    Example         README.md       YunxiaoLibrary.podspec
    LICENSE         YunxiaoLibrary      _Pods.xcodeproj
    wenjun@WendeMacBook-Pro YunxiaoLibrary % git remote add origin https://github.com/WJLollipop/YunxiaoLibrary.git
    wenjun@WendeMacBook-Pro YunxiaoLibrary % git branch -M master
    wenjun@WendeMacBook-Pro YunxiaoLibrary % git push -u origin master
    

    8、打Tag,将Tag推送到远程
    这一步需要注意的是,Tag号要跟podspec中的version保持一致,这里的版本号是0.1.1.

    wenjun@WendeMacBook-Pro YunxiaoLibrary % git tag -a 0.1.1 -m "version 0.1.1"
    wenjun@WendeMacBook-Pro YunxiaoLibrary % git push origin --tags
    Enumerating objects: 1, done.
    Counting objects: 100% (1/1), done.
    Writing objects: 100% (1/1), 171 bytes | 171.00 KiB/s, done.
    Total 1 (delta 0), reused 0 (delta 0)
    To https://github.com/WJLollipop/YunxiaoLibrary.git
     * [new tag]         0.1.1 -> 0.1.1
    

    9、修改podspec文件
    上面的仓库建好了,tag也有了,接下来就是在Xcode中编辑podspec文件。
    各参数的配置,可以参考各字段说明
    这里提一下几个点:

    • 参数后面跟着 R 标记的,代表是Required,是必填的,不然接下去的验证会通不过。
    • s.version要和tag版本一致
    • s.source就是新建的仓库地址
    • s.public_header_files是公开的头文件
    • s.frameworks是需要依赖的系统库
    • s.dependency依赖的三方库

    我的配置如下,供大家参考:

    #
    # Be sure to run `pod lib lint YunxiaoLibrary.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 https://guides.cocoapods.org/syntax/podspec.html
    #
    
    Pod::Spec.new do |s|
      s.name             = 'YunxiaoLibrary'
      s.version          = '0.1.1'
      s.summary          = 'This is a cornner and has a default backgroundColor button.'
    
    # 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: Help developers to creat a cornner button.
                           DESC
    
      s.homepage         = 'https://github.com/WJLollipop/YunxiaoLibrary'
      # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
      s.license          = { :type => 'MIT', :file => 'LICENSE' }
      s.author           = { 'Frank' => 'yunxiao.wen@tuya.com' }
      s.source           = { :git => 'https://github.com/WJLollipop/YunxiaoLibrary.git', :tag => s.version.to_s }
      # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
    
      s.ios.deployment_target = '9.0'
    
      s.source_files = 'YunxiaoLibrary/Classes/**/*'
      
      # s.resource_bundles = {
      #   'YunxiaoLibrary' => ['YunxiaoLibrary/Assets/*.png']
      # }
    
      # s.public_header_files = 'Pod/Classes/**/*.h'
      # s.frameworks = 'UIKit', 'MapKit'
      # s.dependency 'AFNetworking', '~> 2.3'
    end
    

    10、本地验证
    在终端输入pod spec lint [NAME.podspec]。如果有警告,在命令后面加上--allow-warnings

    wenjun@WendeMacBook-Pro YunxiaoLibrary % pod lib lint YunxiaoLibrary.podspec
    
     -> YunxiaoLibrary (0.1.1)
        - WARN  | description: The description is shorter than the summary.
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Building targets in parallel
        - NOTE  | xcodebuild:  note: Using codesigning identity override: -
        - 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')
    
    [!] YunxiaoLibrary did not pass validation, due to 1 warning (but you can use `--allow-warnings` to ignore it).
    You can use the `--no-clean` option to inspect any issue.
    wenjun@WendeMacBook-Pro YunxiaoLibrary % pod lib lint YunxiaoLibrary.podspec --allow-warnings
    
     -> YunxiaoLibrary (0.1.1)
        - WARN  | description: The description is shorter than the summary.
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Building targets in parallel
        - NOTE  | xcodebuild:  note: Using codesigning identity override: -
        - 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')
    
    YunxiaoLibrary passed validation.
    

    11、回到Xcode,进行Pod Install

    wenjun@WendeMacBook-Pro Example % pod install
    Analyzing dependencies
    Downloading dependencies
    Installing YunxiaoLibrary 0.1.1 (was 0.1.0)
    Generating Pods project
    Integrating client project
    Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
    

    可见,本地YunxiaoLibrary 0.1.1已经更新

    12、Push 到 Trunk
    先切换到podspec目录下,执行pod trunk push YunxiaoLibrary.podspec。如果报警告没通过,在命令后加上--allow-warnings

    wenjun@WendeMacBook-Pro YunxiaoLibrary % pod trunk push YunxiaoLibrary.podspec                
    Updating spec repo `trunk`
    Validating podspec
     -> YunxiaoLibrary (0.1.1)
        - WARN  | description: The description is shorter than the summary.
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Building targets in parallel
        - NOTE  | xcodebuild:  note: Using codesigning identity override: -
        - 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')
    
    [!] The spec did not pass validation, due to 1 warning (but you can use `--allow-warnings` to ignore it).
    wenjun@WendeMacBook-Pro YunxiaoLibrary % pod trunk push YunxiaoLibrary.podspec --allow-warnings
    Updating spec repo `trunk`
    Validating podspec
     -> YunxiaoLibrary (0.1.1)
        - WARN  | description: The description is shorter than the summary.
        - NOTE  | xcodebuild:  note: Using new build system
        - NOTE  | xcodebuild:  note: Building targets in parallel
        - NOTE  | xcodebuild:  note: Using codesigning identity override: -
        - 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')
    
    Updating spec repo `trunk`
    
    --------------------------------------------------------------------------------
     🎉  Congrats
    
     🚀  YunxiaoLibrary (0.1.1) successfully published
     📅  April 28th, 22:00
     🌎  https://cocoapods.org/pods/YunxiaoLibrary
     👍  Tell your friends!
    --------------------------------------------------------------------------------
    wenjun@WendeMacBook-Pro YunxiaoLibrary % 
    

    到这,恭喜你创建公开库成功!

    13、验证
    搜索下公开库,看能不能搜到。

    wenjun@WendeMacBook-Pro YunxiaoLibrary % pod search YunxiaoLibrary
    [!] Unable to find a pod with name, author, summary, or description matching `YunxiaoLibrary`
    

    如果报错,请打开: ~/Library/Caches/CocoaPods ,把search_index.json 文件删除再重新 pod search

    wenjun@WendeMacBook-Pro YunxiaoLibrary % pod search YunxiaoLibrary
    [!] Unable to find a pod with name, author, summary, or description matching `YunxiaoLibrary`
    wenjun@WendeMacBook-Pro YunxiaoLibrary % pod search YunxiaoLibrary
    Creating search index for spec repo 'cocoapods'.. Done!
    Creating search index for spec repo 'tuya-inc-tuyaios-tyspecs'.. Done!
    Creating search index for spec repo 'tuya-inc-tuyaios-tyspecsdebug'.. Done!
    Creating search index for spec repo 'tuya-inc-tuyaios-tyspecsrelease'.. Done!
    Creating search index for spec repo 'tuya-inc-tuyaios-tyspecsthird'.. Done!
    Creating search index for spec repo 'tuyainc'.. Done!
    wenjun@WendeMacBook-Pro YunxiaoLibrary % pod search YunxiaoLibrary
    

    相关文章

      网友评论

          本文标题:利用Cocoapods制作公开库

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