spec repo
是pods
的一个索引,是全部公开的pods
的podspec
文件的一个仓库,其实就是一个部署在服务器的Git仓库,当你使用CocoaPods 后它会被Clone到本地的~/.cocoapods/repos
。这个仓库只存放podspec
文件html
一、在git上建立私有仓库地址ios
二、在终端terminal
执行命令git
# pod repo add [Private Repo Name] [GitHub HTTPS clone URL]
$ pod repo add MySpecs https://git.net/winter/MySpecs.git
复制代码
一、建立 Pod 项目工程xcode
# pod lib create [Project Name]
$ pod lib create MyLib
复制代码
而后按照步骤一步一步执行,若是碰到建立失败的状况,更新 cocoaPods 再试试!缓存
二、添加相关代码bash
├── MyLib
│ ├── Assets **存放资源文件!!!**
│ └── Classes
│ └── ReplaceMe.m **注意存放你本身实现的库相关代码!!!**
复制代码
├── Example
│ **就是一个样例工程相关代码文件**
复制代码
三、开发模式下测试 pod 打开Example工程目录下的podfile
文件:服务器
#pod 'MyLib', :path => '../' # 指定路径
pod 'MyLib', :path => '../MyLib.podspec' # 指定podspec文件
复制代码
而后在 Example 工程目录下执行 pod update
命令安装依赖,打开项目工程,能够看到库文件都被加载到Pods子项目中了 不过它们并无在 Pods 目录下,而是跟测试项目同样存在于 Development Pods/MyLib 中,这是由于咱们是在本地测试,而没有把 podspec 文件添加到 Spec Repo 中的缘故。测试库文件没有问题,接着咱们须要执行第4步app
四、提交Pod到代码仓库,注意:不是podspec
索引仓库,是代码仓库 在终端执行命令:ide
$ git add .
$ git commit -s -m "初始化MyLib 库"
$ git remote add origin git@git.net:winter/MyLib.git #添加远端仓库
$ git push origin master #提交到远端仓库
$ git tag -m "first release" "0.1.0" #打上标签,这个很重要
$ git push --tags #推送tag到远端仓库
复制代码
到这里,成功提交到远程代码仓库,MyLib Pod 库就初步完成了代码实现,接下来就是重点了,将制做的私有库放到podspec
索引仓库。测试
podspec
文件到私有Spec Repo
仓库一、配置podspec
文件
#
# Be sure to run `pod lib lint MyLib.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 http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'MyLib'
s.version = '0.1.0'
s.summary = 'MyLib for example'
# 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 = 'http://www.jianshu.com/u/06f42a993882'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'winter' => 'winter.wei@hey900.com' }
s.source = { :git => 'https://git.net/winter/MyLib.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'MyLib/Classes/**/*'
s.resource_bundles = {
'MyLib' => ['MyLib/Assets/*.png']
}
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
end
复制代码
打开MyLib
工程目录下的MyLib.podspec
文件并参考上面的说明配置好相关选项。 podspec
更多配置请参考: 官方文档
二、编辑完MyLib.podspec
文件后,须要验证一下这个MyLib.podspec
文件是否可用
$ pod lib lint
// 若是终端输出这个信息,就说明验证经过,不然会提示错误信息,去修改
-> MyLib (0.1.0)
MyLib passed validation.
复制代码
三、验证经过,想SpecRepo提交podspec
# pod repo push [Repo名] [podspec 文件名字]
$ pod repo push MySpecs MyLib.podspec
复制代码
若是提交成功 终端上就会输出一些相关pod信息,若是不成功,则会打印一些失败信息,切记:失败后,去解决问题,或者更新cocoaPods。
咱们能够直接指定某一个依赖的podspec
,这样就能够使用公司的私有库。该方案有利于使企业内部的公共项目支持CocoaPods。
pod 'MySpec', :podspec => 'https://my.com/mySpec.podspec'
复制代码
须要注意,若是 pod repo push
出现下面的错误信息:
can't get podspec validation - ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code 复制代码
解决办法,能够试试:
1,pod lib lint --allow-warnings
2,解决全部的warning
3,pod repo push <repo-name> <podspec-file-name>.podspec --allow-warnings --use-libraries
复制代码
若是pod lib lint --allow-warnings
出现一些 not find file
之类的warning,必定要解决掉,不然也会出现以上错误信息。
CocoaPods 在执行pod instal
l和pod update
时,会默认先更新一次pod spec
索引,使用--no-repo-update
参数能够禁止其作索引更新操做。
pod install --no-repo-update
pod update --no-repo-update
复制代码
podspec
里面
s.resource_bundles = {
'MyLibrary' => ['your/path/Assets/**/*.{png,xib,plist}']
}
复制代码
通常状况下,咱们本身建立的pods
添加的资源文件,使用[NSBundle mainBundle]
是找不到该资源的路径,因此,在这里,咱们须要建立一个NSBundle
的 category
。
@implementation NSBundle (MyLibrary)
+ (NSBundle *)my_myLibraryBundle {
return [self bundleWithURL:[self my_myLibraryBundleURL]];
}
+ (NSURL *)my_myLibraryBundleURL {
NSBundle *bundle = [NSBundle bundleForClass:[MYSomeClass class]];
return [bundle URLForResource:@"MyLibrary" withExtension:@"bundle"];
}
@end
复制代码
顺便说下,MYSomeClass
这个类能够是任意类名,可是有个前提,这个类必须是在你建立的library
或者framework
内。再说这个逻辑:先拿到最外面的 bundle,对 framework 连接方式来讲是 framework 的 bundle 的根目录,对静态库连接方式来讲就是 target client 的 main bundle,而后再去找下面名为MyLibrary
的 bundle。
上面咱们已经能够正常访问咱们本身的 bundle,若是访问咱们本身 bundle 的图片资源,仍是同样建立UIImage
的category
。
#import "UIImage+MyLibrary.h"
#import "NSBundle+MyLibrary.h"
@implementation UIImage (MyLibrary)
+ (UIImage *)my_bundleImageNamed:(NSString *)name {
return [self my_imageNamed:name inBundle:[NSBundle my_myLibraryBundle]];
}
+ (UIImage *)my_imageNamed:(NSString *)name inBundle:(NSBundle *)bundle {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0
return [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
#elif __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
return [UIImage imageWithContentsOfFile:[bundle pathForResource:name ofType:@"png"]];
#else
if ([UIImage respondsToSelector:@selector(imageNamed:inBundle:compatibleWithTraitCollection:)]) {
return [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
} else {
return [UIImage imageWithContentsOfFile:[bundle pathForResource:name ofType:@"png"]];
}
#endif
}
@end
复制代码
+ imageNamed:inBundle:compatibleWithTraitCollection:
这个方法iOS8之后才有,因此须要条件编译。+ imageWithContentsOfFile:
没有缓存机制。
xxxxx
有时候在安装某一第三方会出现 “Unable to find a specification for xxxxx
” 这个问题,在这里找到了解决方法,只须要把当前Pod的目录清理一下就好了。在终端执行如下命令:
pod repo remove master
pod setup
复制代码
setup(pod setup
可能须要花费很长时间)成功后执行install
或update
便可.