美文网首页iOS收藏
发布自己的开源框架到CocoaPods

发布自己的开源框架到CocoaPods

作者: EvanJq | 来源:发表于2017-07-10 11:25 被阅读24次

简述大致流程

  • 在github上创建项目,复制项目的链接路径,例如:https://github.com/xiayuanquan/XYQCocoaPods.git;
  • 使用命令行或者sourceTree将项目克隆到本地新建的一个文件夹中;
  • cd进入本地该文件夹,将自己之前的工程文件(demo)以及共享文件(共享库Lib)拖入其中,并创建私有仓库,例如:pod spec create cocoaPodsName;
  • 编辑私有仓库信息(使用文本编辑器或者sublime等)
  • 编辑结束保存,并验证本地的私有仓库是否有效(—allow-warnings可以消除警告)例如:pod lib lint cocoaPodsName.podspec —allow-warnings;
  • 验证有效后,然后再将本地该文件夹中所有的文件push到github上
  • 使用git tag(此方法操作后再push上传一次)或者直接在github上点击release进入后创建release并给私有仓库打上tag
  • 注册trunk,例如:pod trunk register 邮箱 ‘用户名’ —descripttion=‘描述’,注意:邮箱为github上的登录邮箱、用户名为github上的用户名
  • 接收发送到邮箱的链接,点击进入后注册成功
  • 查看注册的个人信息,例如:pod trunk me
  • 验证上传到github上的私有仓库是否有效(--allow-warnings可以消除警告,例如:pod spec lint cocoaPodsName.podspec --allow-warnings
  • 将私有仓库推送到CocoaPods上,例如:pod trunk push cocoaPodsName.podspec --allow-warnings
  • 使用pod search cocoaPodsName搜索即可

踩过的坑:

  • 静态库找不到代码文件,请在.podspec文件中source_files栏中加入文件后缀,如cocoaPodsName/Classes/*/.{h,m}' (swift则为.{swift})
  • 如果pod trunk push成功后无法pod search到自己的库,可执行pod setup。
  • 如果仍未pod search到自己的库,在终端执行命令行
    sudo gem install cocoapods 更新pod版本。
  • 如果仍未pod search到自己的库,执行 pod repo update更新本地仓库。

给 Pod 添加资源文件

利用 resource_bundles 属性可以指定 pod 要使用的资源文件。这样就实现了把图片、音频、NIB等资源打包进最终应用程序的目的。

当你的 pod 库以 framework 形式被使用时,你的资源不是被拷贝到 mainBundle 下,而是被放到 pod 的最终产物—— framework 里。此时,你必须保证自己在访问这个 framework 的 bundle,而不是 client target 的。

[NSBundle bundleForClass:<#ClassFromPodspec#>]

上面这段代码可以返回某个 class 对应的 bundle 对象。具体的,

如果你的 pod 以 framework 形式被链接,那么返回这个 framework 的 bundle。
如果以静态库(.a)的形式被链接,那么返回 client target 的 bundle,即 mainBundle。
但无论以哪种形式链接,在这个方法返回的 bundle 下都有你的 pod 资源。接下来要做就是去访问他们。

写了一个方便访问 pod 图片的 category。

spec.resource_bundles = {
  'MyLibrary' => ['your/path/to/resources/*.png'],
}
@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
#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

逻辑很简单:先拿到最外面的 bundle。 对 framework 链接方式来说就是 framework 的 bundle 根目录,对静态库链接方式来说就是 target client 的 main bundle,然后再去找下面名为 MyLibrary 的 bundle 对象,再去取bundle下的对应资源。

最后推荐下自己创建的开源框架JQNavigationController, 解决导航栏透明与变色问题,喜欢的可以给个star。

学习至:
http://www.cnblogs.com/XYQ-208910/p/6214066.html
http://blog.xianqu.org/2015/08/pod-resources/

相关文章

网友评论

    本文标题:发布自己的开源框架到CocoaPods

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