iOS组件化之使用pod命令创建组件

2019-04-17  本文已影响0人  无所不行的刷子

前言

前几篇博客描述了手动创建组件的方法,到新公司发现这种方式不适合小公司快捷方便的需求,所以转而向cocoapods为我们提供的pod lib命令来创建组件,这样对于没接触过组件化的新人比较友好,另外也确实少了很多自己需要配置的各种东西。

准备

创建

1、创建组件命令,你可以CD到特定文件夹下执行命令,创建名为名为Demo的组件为例

# 开始执行以下命令后会去github克隆模板代码没翻墙可能会比较慢
pod lib create  Demo

#创建过程中的选项可参考下面,有特殊也可以自己决定

What platform do you want to use?? [ iOS / macOS ]  平台我们选择iOS
 > iOS

What language do you want to use?? [ Swift / ObjC ]  语言我选择oc
 > ObjC

Would you like to include a demo application with your library? [ Yes / No ]  是否创建demo工程
 > Yes

Which testing frameworks will you use? [ Specta / Kiwi / None ]  是否添加测试支持
 > None

Would you like to do view based testing? [ Yes / No ]  是否添加测试的view
 > No

What is your class prefix?  类名使用的前缀
 > GG

当前命令执行完之后,它会帮我们执行pod install命令并自动打开里面的Example工程,我们先看下创建后文件夹目录:

image.png

pod 'Demo', :path => '../'

在xcode中的工程目录:

image.png

2、创建好之后最好到项目根目录下打开.gitgnore忽略文件中添加下列两行,忽略我们在测试的工程添加的依赖也上传到git上

Example/Pods
Example/Podfile.lock

3、如果有自己的私有库,在Example/Podfile中替换以下添加私有源,没有的可以忽略这步

source 'git@xxxx/Specs.git'

4、上传到仓库,删掉项目根目录下的git文件夹,使用以下命令将文件提交至git,嫌麻烦也可以直接使用Sourcetree来上传

#当组件的根目录下
cd Demo
git init
#关联内网git仓库
git remote add origin git@xxxt/Demo.git
#提交源码
git add .
git commit -m "Initial commit"
git push -u origin master

5、创建源代码文件应注意选择路径,还有对应的target选择

image.png

6、图片的读取问题

image.png
 s.resource_bundles = {
     'Demo' => ['Demo/Assets/Demo.xcassets']
  }
//
//  ResourceManager.h
//  AssetsKit
//
//  Created by GuoMS on 2019/2/18.
//

#import <Foundation/Foundation.h>

/* 使用方法,新建一个文件,头文件写入以下宏定义
 #import "ResourceManager.h"
 //自定义特定的名称
 #undef XXXBundle
 #define XXXBundle [ResourceManager resourceBundleForClass:[self class] podName:@"组件名称"]
 
 #undef BXXXImage
 #define BXXXImage(name) [ResourceManager imageName:name inBundle:XXXBundle]
 */



@interface BQSResourceManager : NSObject


/**
 获取组件的bundle并缓存

 @param aClass 当前调用的类,用于定位bundle位置
 @param name 组件的名字
 @return bundle
 */
+ (NSBundle *)resourceBundleForClass:(Class)aClass
                             podName:(NSString *)name;



/**
 制定bundle获取图片

 @param name 图片名字
 @param bundle bundle实例
 @return 图片、为空返回nil
 */
+ (UIImage *)imageName:(NSString *)name
              inBundle:(NSBundle *)bundle;

@end
//
//  ResourceManager.m
//  AssetsKit
//
//  Created by GuoMS on 2019/2/18.
//

#import "ResourceManager.h"

@interface BQSResourceManager()

@property (strong, nonatomic) NSMutableDictionary *bundleDic;

@end

@implementation ResourceManager

+ (instancetype)sharedInstance {
    
    static BQSResourceManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc]init];
        manager.bundleDic = [NSMutableDictionary dictionary];
    });
    
    return manager;
    
}

+ (NSBundle *)resourceBundleForClass:(Class)aClass podName:(NSString *)name {
    
    NSAssert(aClass, @"class 不能为空");
    NSAssert(name.length > 0, @"组件名称不能为空");
    
    if (name.length <= 0 || !aClass) {
        return nil;
    }
    
    //获取bundle
    NSBundle *cacheBundle = [[BQSResourceManager sharedInstance].bundleDic objectForKey:name];
    if (cacheBundle) {
        return cacheBundle;
    }
    
    NSBundle *bundle = [NSBundle bundleForClass:aClass];
    if (bundle) {
        NSString *resourceBundlePath = [bundle pathForResource:name ofType:@"bundle"];
        if (resourceBundlePath && [[NSFileManager defaultManager] fileExistsAtPath:resourceBundlePath]) {
            bundle = [NSBundle bundleWithPath:resourceBundlePath];
        }
        [[BQSResourceManager sharedInstance].bundleDic setObject:bundle forKey:name];
        
        return bundle;
    }
    
    return nil;
}


+ (UIImage *)imageName:(NSString *)name
              inBundle:(NSBundle *)bundle {
    
    NSAssert(bundle, @"bundle 不能为空");
    NSAssert(name.length > 0, @"图片名称不能为空");
    if (name.length <= 0 || !bundle) {
        return [UIImage new];
    }
    
    UIImage * image = [UIImage imageNamed:name
                                 inBundle:bundle
            compatibleWithTraitCollection:nil];
    
    return image;

}


@end


最后添加了新的头文件之后有时候在Example工程中导入会提示找不到头文件,可以重新执行pod install就可以找到了

如果你有遇到什么问题欢迎在评论中回复,我看到的都会回

上一篇下一篇

猜你喜欢

热点阅读