Cocoapods详情10:让自己的项目开源支持Pod

2022-05-17  本文已影响0人  物非0人非

前提:

安装更新好cocopods,注册好cocopods账号,注册好GitHub账号,注册好git账号,升级下ruby

一、在github上创建一个testProject

重要:记得选择开源协议 (MIT

image.png
二、上传项目源码

直接拖。在电脑上随便一个地方创建一个工程,然后直接推到GitHub仓库里,直接把工程拖进去

image.png
image.png
三、 使用 Github Clone (克隆)项目 到电脑文件夹下(比如我的位置是桌面创建的GitHub文件夹)
image.png image.png

如下放在cocopodsDemo文件目录下

image.png
四、cd到项目当前cocopodsDemo文件夹目录
 cd /Users/ywxk/Desktop/cocopodsDemo 
五、 创建一个podspec文件,命令:
pod spec create testProject

出现 Specification created at testProject.podspec 说明成功

image.png
六、编辑 podspec文件,前端编辑器可以打开(直观好看),我用xocde打开的。删除不必要的注释,修改后 就是这样:

Pod::Spec.new do |spec|



  spec.name         = "testProject"
  spec.version      = "v0.0.1"
  spec.summary      = "一个简述"

  spec.description  = "详细描述"
  #spec.description  = <<-DESC
    #                   DESC

  spec.homepage     = "https://github.com/xiaYingwudi/testProject"
  # spec.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"

  spec.license      = "MIT"
  # spec.license      = { :type => "MIT", :file => "FILE_LICENSE" }

  spec.author             = { "物非-人非" => "835642248@qq.com" }
  # Or just: spec.author    = "物非-人非"
  # spec.authors            = { "物非-人非" => "835642248@qq.com" }
  # spec.social_media_url   = "https://twitter.com/物非-人非"

  # spec.platform     = :iOS
  # spec.platform     = :ios, "5.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"

  spec.source       = { :git => "https://github.com/xiaYingwudi/testProject.git", :tag => s.version }

  # spec.source_files  = "Classes", "Classes/**/*.{h,m}"
   spec.source_files  = 'demo/testProject/**/*'
  # spec.exclude_files = "Classes/Exclude"
  # s.resource_bundles = {  #资源文件,没有就不用写
  #    'SSDemo' => ['SSDemo/Assets/**/*']
  # }

  # spec.public_header_files = "Classes/**/*.h"

  # spec.resource  = "icon.png"
  # spec.resources = "Resources/*.png"

  # spec.preserve_paths = "FilesToSave", "MoreFilesToSave"
  # spec.framework  = "SomeFramework"
  # spec.frameworks = "SomeFramework", "AnotherFramework"

  # spec.library   = "iconv"
  # spec.libraries = "iconv", "xml2"
  # spec.requires_arc = true
  # spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # spec.dependency "JSONKit", "~> 1.4"

end

至于https://github.com/xiaYingwudi/testProject这个就是项目在GitHub上的路径

名次解释
s.name:名称,pod search 搜索的关键词,注意这里一定要和.podspec的名称一样,否则报错
s.version:版本号
s.ios.deployment_target:支持的pod最低版本
s.summary: 简介
s.homepage:项目主页地址
s.license:许可证
s.author:作者
s.social_media_url:社交网址
s.source:项目的地址
s.source_files:需要包含的源文件
s.resources: 资源文件
s.requires_arc: 是否支持ARC
s.dependency:依赖库,不能依赖未发布的库
s.dependency:依赖库,如有多个可以这样写
“” 表示匹配所有文件
“.{h,m}” 表示匹配所有以.h和.m结尾的文件
“**” 表示匹配所有子目录
image.png
七、创建tag,并将其推送到github, 依次一个个执行以下命令:
 git add .
 git commit -m "0.0.1"
 git tag 0.0.1
 git push --tags
 git push origin 0.0.1

上面这几部如果报错,就多执行几次,又可能是网不行。(注: 当执行完 在执行过程中需要输入你的github用户名和密码)

Username for 'https://github.com': github用户名

github用户名: 就是这个GitHub用户名

image.png

github密码: 就是登陆github的的密码

Password for 'https://git用户名@github.com': github密码 

如果出现:

remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: Authentication failed for 'https://github.com/xiaYingwudi/testProject.git/'

意思就是你原先的密码凭证从2021年8月13日开始就不能用了,必须使用个人访问令牌(personal access token),就是把你的密码替换成token
github账号设置里面:

image.png
image.png
image.png
设置token的有效期,要授予此令牌token的范围权限

要使用token命令行访问仓库,请选择repo
要使用token命令行删除仓库,请选择delete_repo
其他根据需要进行勾选,随便选个标记比如:XYCocoPods

image.png image.png

最后生成令牌 Generate token

image.png

注意:

  • 1,记得把你的token复制粘贴保存下来,因为你再次刷新网页的时候,你已经没有办法看到它了,最好保存在你们电脑文档上,下次用直接copy
  • 2,之后用自己生成的token登录,把上面生成的token粘贴到输入密码的位置,然后成功push代码!

需要这么输入命令:

git remote set-url origin  https://你的token@github.com/GitHub用户名/项目工程名.git/

注意:最后的/不能少,否则报错,血泪教训。
例如:

git remote set-url origin  https://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@github.com/xiaYingwudi/demoPodProject.git/

再次执行

git push --tags

出现:

Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 994 bytes | 994.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/xiaYingwudi/demoPodProject.git/
 * [new tag]         0.0.1 -> 0.0.1
xiayin@xiayindeMacBook-Pro demoPodProject % 
八、验证podspec文件
pod spec lint testProject.podspec

这步骤,可能会出错,具体百度下。

nil versions are discouraged and will be deprecated in Rubygems 4
 -> demoPodProject (0.0.1)
    `WARN  | summary: The summary is not meaningful.`
    - ERROR | file patterns: The `source_files` pattern did not match any file.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Planning build
    - NOTE  | xcodebuild:  note: Constructing build description
    - NOTE  | 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  | xcodebuild:  note: Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'App' from project 'App')
    - NOTE  | xcodebuild:  warning: Capabilities for Signing & Capabilities may not function correctly because its entitlements use a placeholder team ID. To resolve this, select a development team in the App editor. (in target 'App' from project 'App')

Analyzed 1 podspec.

[!] The spec did not pass validation, due to 1 error.

WARN | summary: The summary is not meaningful.
这个是警告,意思是说你的summary是系统给生成的描述,你应该改成你自己的。

source_files,路径下没有文件。
这里也可以这么写:

 s.source_files = "Classes/*.{h,m}"

如果同级文件夹内有子文件夹,需要这么写:

 s.source_files = "Classes/**/*.{h,m}"

这样会加载同级文件夹内的所有文件,包括子文件夹内的文件;

如果只需要加载某个子文件夹目录下的文件, 一定要逐级添加.
前往文件目录下:

s.source_files  = "Classes", "Classes/obj/*.{h,m}"

前往工程的pods文件下

/Users/你的Mac账号名/Library/Caches/CocoaPods/Pods/External

如:

/Users/xiayin/Library/Caches/CocoaPods/Pods/External

可以看到这个里面4cdff444...014de这个缓存文件


image.png

然后,终端执行:

//demoPodProject 你的工程仓库名
pod cache clean demoPodProject

然后执行:按照提示一直输:1。把里面的缓存都删除。然后再执行

// demoPodProject 你的工程仓库名
pod spec lint demoPodProject.podspec

然后在上面生成的文件下面导入你要pod的文件


image.png

spec里面

  spec.source_files  = 'Classes/*.{h,m}'

再执行

// demoPodProject 你的工程仓库名
pod spec lint demoPodProject.podspec

报错:


nil versions are discouraged and will be deprecated in Rubygems 4
 -> demoPodProject (0.0.1)
    - ERROR | xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Planning build
    - NOTE  | xcodebuild:  note: Constructing build description
    - NOTE  | xcodebuild:  demoPodProject/Classes/DLCDiscoverVC.h:9:9: fatal error: 'DLCBaseViewController.h' file not found
    - NOTE  | xcodebuild:  warning: Capabilities for Signing & Capabilities may not function correctly because its entitlements use a placeholder team ID. To resolve this, select a development team in the App editor. (in target 'App' from project 'App')
    - NOTE  | [watchOS] xcodebuild:  fatal error: 'DLCBaseViewController.h' file not found

Analyzed 1 podspec.

[!] The spec did not pass validation, due to 1 error.

这个是执行 pod spec lint指令的时候产生的错误:代码逻辑不能有错,比如你import一个不存在的文件,就会报错,检查下代码,导入的.h的头文件都存在就行,run起来,能成功就行。

image.png
九、 如果是第一次提交,需要先执行这个命令
pod trunk register 这里写邮箱 '这里起个名字' --description=' 这里写描述'

比如:

pod trunk register '2442285459@qq.com' --description=' xxxx描述'

执行完成之后,会给你的邮箱里发一封邮件,去邮箱点击链接,就行

然后提交到CocoaPods

(注意:由于我验证podspec文件时使用了--allow-warnings 所以这里后面也加上--allow-warnings)

pod trunk push demoPodProject.podspec --allow-warnings

再执行成功了。

image.png image.png
十、 提交完成后,就可以通过cocopods查找demoPodProject了
image.png

最后

pod setup 
image.png

此刻验证是否成功:

pod search demoPodProject

如果能搜到,恭喜你成功了。

如果用pod search 命令会搜不到,因为本地的索引没有更新,使用下面命令进入文件目录,删除索引文件
search_index.json


image.png
rm ~/Library/Caches/CocoaPods/search_index.json

最后pod search demoPodProject 命令搜到自己的项目,大功告成,如果还是不行,终端输入:

rm ~/Library/Caches/CocoaPods/search_index.json

执行:

pod setup

再进行pod search demoPodProject,还是不行。那么就需要:
a、在终端输入cd ~/.cocoapods/repos
b、移除master文件

git clone --depth 1 https://github.com/CocoaPods/Specs.git master

再次搜索试试pod search demoPodProject,稍等片刻即可成功。

常见问题

1加载xib问题
如果通过cocoapods下载的类库中含有Xib文件, 使用原来的方式初始化就不起作用了:

[[[NSBundle mainBundle] loadNibNamed:@"xibName" owner:self options:nil] lastObject];
[self.collectionView registerNib:[UINib nibWithNibName:@"xibName" bundle:nil] forCellWithReuseIdentifier:@"ZLCollectionCell"];

在spec资源路径里面需要加上xib类型,同同时应该使用下面这种方式初始化:

#define kZLPhotoBrowserBundle [NSBundle bundleForClass:[self class]]
[[kZLPhotoBrowserBundle loadNibNamed:@"ZLPhotoActionSheet" owner:self options:nil] lastObject];
[self.collectionView registerNib:[UINib nibWithNibName:@"ZLCollectionCell" bundle:kZLPhotoBrowserBundle] forCellWithReuseIdentifier:@"ZLCollectionCell"];

2加载图片资源问题
设置了s.resources = "LZScaner/images/.png"* , 图片也能正常显示( 可参考我设置的podspec文件LZScaner.podspec )没有遇到这个问题, 但是还是把这个方式记录一下吧...

如果通过代码"[UIImage imageNamed:@"picName"]" 去设置图片的话,则图片资源有可能无法正常显示, 可通过以下方式解决:

创建bundle资源目录

command+N -> Resource -> Settings Bundle

删除bundle携带的无用文件,把图片资源添加到bundle资源内
改变代码图片路径

// 图片路径
#define kZLPhotoBrowserSrcName(file) [@"ZLPhotoBrowser.bundle" stringByAppendingPathComponent:file]
#define kZLPhotoBrowserFrameworkSrcName(file) [@"Frameworks/ZLPhotoBrowser.framework/ZLPhotoBrowser.bundle" stringByAppendingPathComponent:file]
kZLPhotoBrowserSrcName(file) 为通过copy文件夹方式获取图片路径的宏
kZLPhotoBrowserFrameworkSrcName(file) 为通过cocoapods下载安装获取图片路径的宏

然后修改代码中设置图片的方式如下

UIImage *img = [UIImage imageNamed:kZLPhotoBrowserSrcName(@"img.png")]?:[UIImage imageNamed:kZLPhotoBrowserFrameworkSrcName(@"img.png")];
上一篇 下一篇

猜你喜欢

热点阅读