iOS 组件化

组件化遇到的问题

2018-11-27  本文已影响8人  Misaki_yuyi

    来公司大半年时间了,一个人搞组件化不轻松,先来看看podfile,业务功能都已经拆分成了一个组件,现在主工程就是一个空壳子,所有的业务场景都在组件化包里面。现在总结一下,在组件化过程中遇到的问题。

source 'https://git.XXX.com/products/paas-app-ios-spec.git'
source 'https://github.com/CocoaPods/Specs.git'
# platform :ios, '8.0'

target 'YYPaas' do
pod 'PaasBaseComponent', '~> 1.0.30'

#pod 'PaasUMComponent', '~> 1.0.1'
pod 'PaasAddressComponent', '~> 1.0.7'
pod 'PaasThemeBuildStyleComponent', '~> 1.0.1'
pod 'PaasUMBuildStyleComponent', '~> 1.0.15'
pod 'PaasCollectionComponent', '~> 1.0.1'
pod 'PaasFootprintComponent', '~> 1.0.2'
pod 'PaasIntegralComponent', '~> 1.0.4'
pod 'PaasUserInfoComponent', '~> 1.0.1'
pod 'PaasSettingComponent', '~> 1.0.3'
pod 'PaasOrderComponent', '~> 1.0.5'
pod 'PaasAfterSaleComponent', '~> 1.0.1'


#use_frameworks!

# Pods for YYPaas

创建私有库没有通过远端校验问题

pod spec lint --sources='https://git.xxxx.com/products/paas-app-ios-spec.git,https://github.com/CocoaPods/Specs' --use-libraries --allow-warnings --verbose

    一般创建私有库的时候,本地校验都能通过,但是远端校验通不过,报错下图的错误


pod spec lint 报错.png

    上网查原因是上传的包过大,http的头部错误导致。后来修改postBuffer改为50M,100M都,但是还是不起作用。后来通过修改私有库的.podspec文件来解决这个问题。

    本地校验指定source路径为https

s.source           = { :git => 'https://git.xxx.com/yuyi/PaasAddressComponent.git', :tag => s.version.to_s }

    提交代码至远端后修改sourcessh方式,然后进行pod spec lintpod repo push

s.source           = { :git => 'ssh://git@git.xxx.com:3589/yuyi/PaasAddressComponent.git', :tag => s.version.to_s }

    pod repo push 操作后要记住把s.source的引入方式改为https,不然通过不了本地校验。
    私有库都托管到公司的gitlab上面,现在还没有解决这个问题,我想应该是公司gitLab设置的问题,毕竟在同样的私有库托管在https://gitee.com/或者自己的gitlab账号都没有出现过同样的问题。

资源文件的引用问题

    搭建页面的时候都是用Masonry代码布局,没有用xib文件。这里的资源文件自剩下图片资源了。
    假如不在组件中,图片资源交给Assets.xcassets来管理,引用直接通过[UIImage imageNamed:@"paas_nodata_placeholder"]
    现在没有单独的组件包的图片资源就不会放在Assets.xcassets,而是通过放在组件Assets目录下来管理,引用方式也变成了[UIImage imageWithContentsOfFile:path]

+ (UIImage *)imageWithPaasComponentType:(PaasComponentType)type
                          WithClassName:(Class)className
                               WithName:(NSString *)imageName
{
    NSBundle * bundle = [NSBundle bundleForClass:className];
    NSString * componentName;
    switch (type) {
        case PaasComponentType_Base:
            componentName = @"PaasBaseComponent";
            break;
        case PaasComponentType_UM:
            componentName = @"PaasUMComponent";
            break;
        case PaasComponentType_Address:
            componentName = @"PaasAddressComponent";
            break;
        case PaasComponentType_BuildUM:
            componentName = @"PaasUMBuildStyleComponent";
            break;
        case PaasComponentType_Collection:
            componentName = @"PaasCollectionComponent";
            break;
        case PaasComponentType_Footprint:
            componentName = @"PaasFootprintComponent";
            break;
        case PaasComponentType_Integral:
            componentName = @"PaasIntegralComponent";
            break;
        case PaasComponentType_UserInfo:
            componentName = @"PaasUserInfoComponent";
            break;
        case PaasComponentType_Setting:
            componentName = @"PaasSettingComponent";
            break;
        case PaasComponentType_Order:
            componentName = @"PaasOrderComponent";
            break;
        case PaasComponentType_AfterSale:
            componentName = @"PaasAfterSaleComponent";
            break;
        case PaasComponentType_Evaluation:
            componentName = @"PaasEvaluationComponent";
            break;
            
        default:
            break;
    }
    NSString * resource = [NSString stringWithFormat:@"%@.bundle/%@.png",componentName,[self addSuffixWithImageName:imageName]];
    NSString * path = [bundle pathForResource:resource ofType:nil];
    return [UIImage imageWithContentsOfFile:path];
}

+ (NSString *)addSuffixWithImageName:(NSString *)imageName
{
    NSString * fileName;
    if ([UIScreen mainScreen].scale == 3)
    {
        fileName = [NSString stringWithFormat:@"%@@3x",imageName];
    }
    else
    {
        fileName = [NSString stringWithFormat:@"%@@2x",imageName];
    }
    return fileName;
}

    首先根据className来确定所在的Bundle,然后根据PaasComponentType来确定所在的组件名称,最后用[UIScreen mainScreen].scale来确定是用@2x图片还是@3x图片。

    podspec加入图片文件

组件化图片引入方式.png
    s.resource_bundles = {
        'PaasAddressComponent' => ['PaasAddressComponent/Assets/*.png']
    }

组件之间的通讯问题

    我在拆分订单组件和售后组件之前,订单详情是这样跳转到申请售后的

RefundViewController * vc = [[RefundViewController alloc]init];
vc.FmodelArray = self.modelArray
[self.navigationController pushViewController:vc animated:YES];

    OrderShopGoodsModel是商品的modelself.modelArray是商品详情存放OrderShopGoodsModel的集合,RefundViewController对外暴露了一个FmodelArray数组,里面存放的是OrderShopGoodsModel集合。
    后来为了解耦,创建了RefundShopGoodsModelOrderShopGoodsModelRefundShopGoodsModel是没有任何区别的,都是用来存放商品的信息,例如 id、name、sku、imageUrl、price等。converRefundGoodsModelWithArray用来把OrderShopGoodsModel转化成RefundShopGoodsModel

vc.FmodelArray = [self converRefundGoodsModelWithArray:self.modelArray];

    这样没有根本解决解耦问题,把转换Model的代码放在订单详情,订单详情还需要依赖RefundShopGoodsModel,同样把转换的代码放在申请售后,售后模块还要依赖OrderShopGoodsModel
    为了解决模块之间通讯的参数问题,是不能用Model来传递的,否则无法解决依赖问题。最后的做法是RefundViewController暴露出一个字典Fparams用来接收参数。Fparams是未经转化原始商品数据。然在RefundViewController内写方法将字典转为相应的模型。

vc.Fparams = self.refundParams;

    配合中间件CTMediator路由跳转如下

UIViewController * vc = [[CTMediator sharedInstance] CTMediator_RefundViewControllerWithValue:self.refundParams];
[self.navigationController pushViewController:vc animated:YES];
上一篇 下一篇

猜你喜欢

热点阅读