条件:
安装更新好cocopods,注册好cocopods账号,注册好GitHub账号,注册好git账号,升级下ruby。
一、在github上创建一个testProject
告急:记得选择开源协议 (MIT)
二、上传项目源码
直接拖。在电脑上任意一个地方创建一个工程,然后直接推到GitHub堆栈里,直接把工程拖进去
三、 利用 Github Clone (克隆)项目 到电脑文件夹下(好比我的位置是桌面创建的GitHub文件夹)
如下放在cocopodsDemo文件目次下
四、cd到项目当前cocopodsDemo文件夹目次
cd /Users/ywxk/Desktop/cocopodsDemo 五、 创建一个podspec文件,下令:
pod spec create testProject出现 Specification created at testProject.podspec 分析乐成
六、编辑 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: 是否支持ARCs.dependency:依赖库,不能依赖未发布的库s.dependency:依赖库,如有多个可以如许写“” 表示匹配全部文件“.{h,m}” 表示匹配全部以.h和.m末了的文件“**” 表示匹配全部子目次七、创建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用户名
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账号设置内里:
设置token的有用期,要授予此令牌token的范围或权限。
要利用token从下令行访问堆栈,请选择repo。
要利用token从下令行删除堆栈,请选择delete_repo
其他根据需要举行勾选,任意选个标记好比:XYCocoPods
末了天生令牌 Generate token
留意:
- 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 threadsCompressing 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.1xiayin@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是系统给天生的形貌,你应该改成你本身的。
- ERROR | file patterns: The source_files pattern did not match any file.
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这个缓存文件
然后,终端实行:
//demoPodProject 你的工程堆栈名pod cache clean demoPodProject然后实行:按照提示不绝输:1。把内里的缓存都删除。然后再实行
// demoPodProject 你的工程堆栈名pod spec lint demoPodProject.podspec然后在上面天生的文件下面导入你要pod的文件
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 foundAnalyzed 1 podspec.[!] The spec did not pass validation, due to 1 error.这个是实行 pod spec lint指令的时间产生的错误:代码逻辑不能有错,好比你import一个不存在的文件,就会报错,检查下代码,导入的.h的头文件都存在就行,run起来,能乐成就行。
九、 如果是第一次提交,需要先实行这个下令
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再实行乐成了。
十、 提交完成后,就可以通过cocopods查找demoPodProject了
末了
pod setup 现在验证是否乐成:
pod search demoPodProject如果能搜到,恭喜你乐成了。
如果用pod search 下令会搜不到,由于本地的索引没有更新,利用下面下令进入文件目次,删除索引文件
search_index.json
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")]; |