iOS | 给项目添加pods支持

2023-03-09  本文已影响0人  SFHinc

目录

1、创建远程库

2、提交项目代码并打好tag标签

3、创建并编辑.podspec索引文件

4、把.podspec提交到Cocoapods trunk

5、可能会遇到的问题

1、创建远程库

登录自己的github账号(其他平台类似),创建一个新的repository, 添加README、.gitignore和license,点击下方的绿色按钮创建远程仓库

创建一个开源的repository.png

如果要删除某个库,找到这个库的Settings,删除库的入口在最底部。

2、提交项目代码并打好tag标签

远程库与本地项目关联有两种方法:
第一种,在本地新建文件夹TestKit,使用git clone命令把远程库clone到本地,在文件夹下创建或添加项目。

//终端切换到文件夹TestKit目录下
 cd /Users/userName/Desktop/TestKit 

//clone远程库
 git clone https://github.com/XXX/TestKit.git

//把项目文件放到新建的文件夹TestKit下
...

//提交当前文件夹下的所有文件到暂存区
git add .

//提交
git commit -m "提交说明"

//上传远程库,master是远程分支名称
git push origin master

第二种,在项目根目录下使用git remote add命令把远程库和本地项目关联起来。

//初始化git仓库
git init

//提交当前文件夹下的所有文件到暂存区
git add .

//提交
git commit -m "提交说明"

//关联远程库
git remote add origin https://github.com/XXX/TestKit.git

//指定origin为默认主机,如果加了这一步,上传远程库可以不带-u参数
git remote

//上传远程库,master是远程分支名称
//本地分支推送到origin主机,同时指定origin为默认主机,
//后面就可以不加任何参数使用git push,
//也可解决git建立远程关联时出现fatal ... upstram的问题
git push -u origin master

git仓库是隐藏文件,如果创建后项目根目录下看不到,使用快捷键shift+cmd+.可显示或隐藏文件,或使用命令

//显示
defaults write com.apple.finder AppleShowAllFiles -boolean true; killall Finder
//隐藏
defaults write com.apple.finder AppleShowAllFiles -boolean false; killall Finder

打tag标签,标签要与后面的podspec文件里的version参数一致

//打本地标签
git tag v1.0.0

//提交标签到远程
git push --tags
//初始化git仓库
git init 

//添加当前文件夹下的所有文件到暂存区                           
git add .

//查看状态,红色字体表示本地改动没有提交,绿色字体表示已提交到了暂存区没有commit                                       
git status

//提交
git commit

//提交带提交说明,如果第一次使用,会提示输入github的账号密码                                
git commit -m ‘注释’   

//查看提交日志             
git log

//更新remote索引
git fetch

//推送
git push

//推送到远程master分支
git push origin master 

//本地分支推送到origin主机,同时指定origin为默认主机,
//后面就可以不加任何参数使用git push,
//也可解决git建立远程关联时出现fatal ... upstram的问题
git push -u origin master 

//按标签推送到远程
git push origin ‘标签,比如v1.0.0’ 

//关联远程仓库               
git remote add origin <URL>

//打标签
git tag '版本'

//打标签带描述
git tag -a '版本' -m '描述'

//标签提交到远程 
git push --tags

//删除本地标签
git tag -d v0.0.1

//远程删除标签
git push origin :v0.0.1

//查看当前所有分支
git branch        

//创建分支
git branch dev              

//检出分支
git checkout dev

//创建并检出分支
git checkout -b dev

//查看分支及hash值和提交信息
git branch -v

//把dev分支合并到当前分支
git merge dev

//删除分支dev
git branch -d dev

//强制删除分支dev,若没有其他分支合并就删除,-d会有提示 -D不会提示
git branch -D dev

//修改分支,分支dev改为kdev
git branch -m dev kdev

//强制修改分支,与其他分支有冲突也会创建
git branch -M dev kdev

3、创建并编辑.podspec索引文件

podspec官方语法说明 Podspec Syntax Reference

在项目根目录下创建podspec索引文件,文件名与项目名一致

//比如现有项目TestKit
pod spec create TestKit
或
pod spec create TestKit.podspec

然后编辑这个文件

Pod::Spec.new do |spec|

  # ―――  基本信息  ―――  #
  #spec文件名,也是使用pod搜索或安装时用的名字
  spec.name         = "TestKit"  
  # spec版本号
  spec.version      = "1.0.0"  
  # 简介
  spec.summary      = "常用组件"
  # 描述,description内容比summary要长一些,否则会有警告
  spec.description  = "常用组件和扩展"
  # 主页
  spec.homepage     = "https://github.com/huhansan/TestKit"   
  # spec.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"

  # ―――  开源协议  ―――#
  # spec.license      = "MIT"   
  spec.license      = { :type => "MIT", :file => "LICENSE" }

  # ―――  作者信息  ―――#
  spec.author       = { "huhansan" => "123456789@gmail.com" }   
  # Or just: spec.author    = "huhansan"
  # spec.authors            = { "huhansan" => "123456789@gmail.com" }
  # spec.social_media_url   = "http://twitter.com/huhansan"

  # ―――  平台信息  ―――#
  # spec.platform     = :ios
  # 平台及使用版本,ios平台,系统版本11.0及以上
  spec.platform     = :ios, "11.0"
  #  When using multiple platforms
  # spec.ios.deployment_target = "5.0"
  # spec.osx.deployment_target = "10.7"
  # spec.watchos.deployment_target = "2.0"
  # spec.tvos.deployment_target = "9.0"

  # ―――  git源信息  ―――#
  # 下面几种写法都可以,注意tag值,要保证与项目提交的tag一致:
  //指定tag值
  # spec.source = { :git => "https://github.com/huhansan/TestKit.git", :tag => 1.0.0 }
  //取spec.version
  # spec.source = { :git => "https://github.com/huhansan/TestKit.git", :tag => spec.version }    
  //tag带有前缀 "v" 
  spec.source = { :git => "https://github.com/huhansan/TestKit.git", :tag => "v#{spec.version}" } 
  //版本hash值
  # spec.source = { :git => "https://github.com/huhansan/TestKit.git", :commit => "c8706d6" }
  
  # ―――  资源管理  ―――#
  # 代码文件
  // “*” 表示匹配所有文件
  // “*.{h,m}” 表示匹配所有以.h和.m结尾的文件
  // “**” 表示匹配所有子目录
  spec.source_files  = "TestKit/Classes/**/*.{h,m,swift,xib,xcassets}"

  # ―――  资源信息  ―――# 
  # spec.resource  = "icon.png"
  # spec.resources = "Resources/*.png"
  # spec.preserve_paths = "FilesToSave", "MoreFilesToSave"

  # 使用resources方式引用资源文件
  spec.resources = ['Assets/Images/*.png', 'Assets/Resource/*']
  # 使用spec.resources方式是将资源文件copy到目标工程(Example工程)
  # 最后和目标工程的图片文件以及其他同样使用resources的Pod的图片文件,统一打包为一个Assets.car
  # 优点:可以使用 .xcassets 指定资源文件、不需要用硬编码方式获取图片
  # 缺点:可能会导致每个库和主工程之间的同名资源冲突
  # 硬编码方式:[NSBundle bundleForClass:[self class] compatibleWithTraitCollection:nil];


  # 资源文件打包成bundle,避免资源文件冲突
  spec.resource_bundles = {
      'TestKitBundle' => ['TestKit/Classes/**/*.storyboard'],
      'TestKitImgBundle' => ['Assets/Images/*.png']
  }
  # 使用spec.resource_bundles方式是会为指定的资源打一个.bundle,.bundle包含一个Assets.car,
  # 获取图片的时候要严格指定.bundle的位置,很好的隔离了各个库或者一个库下的资源包。避免资源同名冲突。
  # 优点:可以使用.xcassets指定资源文件、可以避免每个库和主工程之间的同名资源冲突
  # 缺点:获取图片时可能需要使用硬编码的形式来获取,比如想访问TestResourceBundle的资源:
  # [NSBundle bundleForClass:[self class].resourcePath stringByAppendingPathComponent:@"/TestResourceBundle.bundle"]


  # 不包含资源
  spec.exclude_files = "Classes/Exclude"
  # 对外开放的头文件地址,如果不指定会默认加载源代码所有头文件
  spec.public_header_files = "AFNetworking/*.h" #静态库时必须有
  
  # ―――  工程链接库  ―――# 
  # 引用系统公有framework,用“,”分割,去掉后缀 .framework
  spec.framework  = "UIKit"
  spec.frameworks = "UIKit", "AnotherFramework"

  # 引用系统静态库,去掉头尾的lib,用“,”分割
  spec.library   = "z"
  spec.libraries = "iconv", "xml2"
  
  # 引用自己生成的framework,用“,”分割 
  # 路径写从.podspec所在目录为根目录的相对路径,带后缀 .framework
  spec.vendored_frameworks = 'TestKit/Frameworks/*.framework'

  # 引用自己生成的.a静态库(必须以lib打头)
  spec.vendored_libraries = "TestKit/Libraries/libTestKit.a"

  # ――― 工程配置 ――― #
  # 是否是arc模式
  spec.requires_arc = true  

  # spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  
  # 依赖的第三方类库
  # spec.dependency "JSONKit", "~> 1.4"
  spec.dependency "RxSwift"
  spec.dependency "RxCocoa"
  ...

  # 配置xcode的other flag
  spec.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '$(inherited) -undefined dynamic_lookup' }
  # spec.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lObjC' }

  # 指定SDK为静态库
  spec.static_framework  =  true

  # ――― 类库依赖 ――― #
  # 将单独的功能拆分为一个子模块,只想用子模块功能的时候只需在pod中引入子模块,不需要引入库的所有文件

  //子模块Reachability
  spec.subspec 'Reachability' do |ss|
    ss.source_files = 'TestKit/Classes/Reachability/**/*.{h,m,swift}'
    ss.dependency "AFNetworking/Reachability", '~> 3.0'
  end

  //子模块Downloader
  spec.subspec 'Downloader' do |down|
    down.source_files = 'TestKit/Classes/DownLoader/**/*'
    down.dependency "AFNetworking"
  end
  # Podfile中配置:
  # pod 'TestKit', '~> 1.0.0', :subspecs => ["Reachability", "Downloader"]
  # 或
  # pod 'TestKit/Reachability', '~> 1.0.0'
  # pod 'TestKit/Downloader', '~> 1.0.0'

end

提交前验证.podspec文件的合法性,也可以跳过验证环节直接提交,提交时会先验证本地.podspec文件的合法性再上传到trunk,最后会将上传的.podpec文件转换为需要的json文件。

# 无需联网
pod lib lint TestKit.podspec 
或
# 联网检查sepc repo并关联tag
pod spec lint TestKit.podspec 

如果验证有警告可以用--allow-warnings忽略

pod spec lint TestKit.podspec --allow-warnings

如果出现错误但是提示信息不足,可以添加--verbose以获取更多错误信息

pod spec lint TestKit.podspec --verbose

.podspec应该在没有任何错误或警告的情况下通过验证。若验证过程中有错误,验证失败,修改后再次验证,直到通过验证。

Usage:

    $ pod lib lint [PODSPEC_PATHS ...]

      Validates the Pod using the files in the working directory.

Options:
 --quick                                  Lint skips checks that would require to
                                             download and build the spec
    --allow-warnings                         Lint validates even if warnings are
                                             present
    --subspec=NAME                           Lint validates only the given subspec
    --no-subspecs                            Lint skips validation of subspecs
    --no-clean                               Lint leaves the build directory intact
                                             for inspection
    --fail-fast                              Lint stops on the first failing platform
                                             or subspec
    --use-libraries                          Lint uses static libraries to install the
                                             spec
    --use-modular-headers                    Lint uses modular headers during
                                             installation
    --use-static-frameworks                  Lint uses static frameworks during
                                             installation
    --sources=https://cdn.cocoapods.org/     The sources from which to pull dependent
                                             pods (defaults to
                                             https://cdn.cocoapods.org/). Multiple
                                             sources must be comma-delimited
    --platforms=ios,macos                    Lint against specific platforms (defaults
                                             to all platforms supported by the
                                             podspec). Multiple platforms must be
                                             comma-delimited
    --private                                Lint skips checks that apply only to
                                             public specs
    --swift-version=VERSION                  The `SWIFT_VERSION` that should be used
                                             to lint the spec. This takes precedence
                                             over the Swift versions specified by the
                                             spec or a `.swift-version` file
    --include-podspecs=**/*.podspec          Additional ancillary podspecs which are
                                             used for linting via :path
    --external-podspecs=**/*.podspec         Additional ancillary podspecs which are
                                             used for linting via :podspec. If there
                                             are --include-podspecs, then these are
                                             removed from them
    --skip-import-validation                 Lint skips validating that the pod can be
                                             imported
    --skip-tests                             Lint skips building and running tests
                                             during validation
    --test-specs=test-spec1,test-spec2,etc   List of test specs to run
    --analyze                                Validate with the Xcode Static Analysis
                                             tool
    --configuration=CONFIGURATION            Build using the given configuration
                                             (defaults to Release)
    --allow-root                             Allows CocoaPods to run as root
    --silent                                 Show nothing
    --verbose                                Show more debugging information
    --no-ansi                                Show output without ANSI codes
    --help                                   Show help banner of specified command

4、把.podspec提交到Cocoapods trunk

注册CocoaPods trunk账户,需要一个真实可用的邮箱用于邮件验证

pod trunk register 123456789@gmail.com 'huhansan'

也可在用户名后面跟描述信息

pod trunk register 123456789@gmail.com 'huhansan' --description='huhansan的trunk' --verbose

注册命令执行后会在对应邮箱收到验证邮件,点击验证链接完成验证,提交.podspec文件到trunk

pod trunk push TestKit.podspec

使用--allow-warnings忽略警告

pod trunk push TestKit.podspec --allow-warnings

如果使用了静态库或私有库中有.a文件,必须加上--use-libraries

pod trunk push TestKit.podspec --use-libraries --allow-warnings

提交成功后更新下本地cocoapods再使用pod search命令搜索

pod setup
...
pod search TestKit

如果搜索不到就清理下cocoapods的搜索索引文件search_index.json的本地缓存,然后再搜索

rm ~/Library/Caches/CocoaPods/search_index.json 
...
pod search TestKit
# 注册trunk  -  pod trunk register [EMAIL] [USERNAME]
$pod trunk register 123456789@gmail.com 'huhansan'

# 查看已注册信息  -  pod trunk me
# 包含账号的Name、Email、Since、Pods、Sessions,其中Pods为该账号向CocoaPods提交的所有的库
$pod trunk me

- Name:     huhansan
- Email:    123456789@gmail.com
- Since:    February 8th, 02:15
- Pods:
  - HHSKit
  - HHSUIKit
  - HHSComKit
  - HHSActionSheet
  - HHSProgressHUD
  - HHSDownloadManager
  - HHSNavigationController
  - HHSTabBarController
- Sessions:
  - April 1st, 05:09 -  August 7th, xx:22. IP: xxx.xxx.xx.xxx
  - April 4th, 20:44 - August 10th, xx:58. IP: xxx.xxx.xx.xxx
  - April 4th, 21:37 - August 12th, xx:33. IP: xxx.xxx.xx.xxx
  Description: huhansan的trunk

# 查看账号下某个库的信息  -  pod trunk info [POD REP NAME]
$pod trunk info HHSUIKit

HHSUIKit
 - Versions:
 - 1.0.1 (2015-11-09 05:43:23 UTC)
 - 1.0.2 (2016-03-12 12:08:47 UTC)
 - 1.0.5 (2017-10-18 08:14:53 UTC)
 - 1.0.6 (2018-11-25 16:28:27 UTC)
 - 1.1.0 (2019-12-07 04:49:24 UTC)
 - 1.1.1 (2020-12-09 11:12:05 UTC)
 - 1.1.3 (2021-12-16 10:18:15 UTC)
 - 1.1.5 (2022-02-17 10:59:12 UTC)
 - Owners:
 - huhansan 

# 不管添加还是删除某个库的拥有者,必须`pod trunk register`登录过并且操作的是登录账号所拥有的库
// 添加某个库的拥有者  -  pod trunk add-owner [POD REP NAME] [OWNER-EMAIL]
$pod trunk add-owner HHSUIKit 123456789@gmail.com

// 删除某个库的拥有者  -  pod trunk remove-owner [POD REP NAME] [OWNER-EMAIL]
$pod trunk remove-owner HHSUIKit 123456789@gmail.com

# 让某个库过期 -  pod trunk deprecate [POD REP NAME]
$pod trunk deprecate HHSUIKit

# 删除某个库的某个版本,该命令无法撤回  -  pod trunk delete [POD REP NAME] [VERSION]
$pod trunk delete HHSUIKit 1.0.1

# 发布库,path是PodRepName.podspec的路径,若处于同一级目录也可不加path  -  pod trunk push [PATH]
$pod trunk push HHSUIKit.podspec
...
done

5、可能会遇到的问题

未完待读...

上一篇下一篇

猜你喜欢

热点阅读