iOS第三方平台集成组件化续集(以插件化的方式添加自定义的第三方平台)

背景

不久前写了一篇 iOS第三方平台集成组件化 ,刚好最近公司又在做项目的壳版本,所以考虑把项目中的一些模块做成组件化,这么做的好处是基础的技术组件和基础的业务数据提供组件可以在多个壳版本中共用,不同的项目中进行相关的配置即可,以达到减少工作量和提高效率的目的,此外对于项目的以后的维护也是有帮助的,如果出问题了,只需要修改某一部分的组件即可而不需要修改到其他地方。
在整个项目重构的过程中,发现很多的模块耦合的很厉害,包括我上次做的 iOS第三方平台集成组件化 ,所以特别地我又把这部分做了一个调整,以便于更好的解耦和复用。因为改动的地方比较多,所以新写一篇文章来记录和分享。

结果

在旧的组件库的基础上实现了

  • 库项目和业务解耦
  • 可配置化
  • 独立的私有的Pod组件
  • 系统库依赖零配置
  • 更简洁的代码和目录结构
  • 子模块化第三方平台的业务逻辑
  • 以插件化的方式添加自定义的第三方平台

我是代码,欢迎点我👉👉YTThirdPlatformManager

第三方平台注册的例程
注册不同平台的APPID、APPKEY、APPSecret等信息

// 第三方平台注册
[[PTThirdPlatformManager sharedInstance] setPlaform:PTThirdPlatformTypeWechat appID:kWXAppID appKey:nil appSecret:kWXAppSecret redirectURL:nil];
[[PTThirdPlatformManager sharedInstance] setPlaform:PTThirdPlatformTypeTencentQQ appID:kTencentAppID appKey:kTencentAppKey appSecret:kTencentAppSecret redirectURL:nil];
[[PTThirdPlatformManager sharedInstance] setPlaform:PTThirdPlatformTypeWeibo appID:kWeiboAppID appKey:kWeiboAppKey appSecret:kWeiboAppSecret redirectURL:kWeiboRedirectURI];
[[PTThirdPlatformManager sharedInstance] setPlaform:PTThirdPlatformTypeAlipay appID:nil appKey:nil appSecret:nil redirectURL:nil];
[[PTThirdPlatformManager sharedInstance] thirdPlatConfigWithApplication:application didFinishLaunchingWithOptions:launchOptions];

第三方平台调用的例程
这部分和之前的一样

 [self addActionWithName:@"QQ Login" callback:^{
        [[PTThirdPlatformManager sharedInstance] signInWithType:PTThirdPlatformTypeTencentQQ fromViewController:weakSelf callback:^(ThirdPlatformUserInfo *userInfo, NSError *err) {
        }];
    }];
    
    [self addActionWithName:@"Wechat Login" callback:^{
        [[PTThirdPlatformManager sharedInstance] signInWithType:PTThirdPlatformTypeWechat fromViewController:weakSelf callback:^(ThirdPlatformUserInfo *userInfo, NSError *err) {
        }];
    }];

思路和实现

库项目和业务解耦

旧的组件在配置方面和业务是没有解耦的,第三方平台的配置是通过 PTThirdPlatformConfigConst 硬编码实现各自平台的配置的,所以参考了友盟分享组件的方式,添加了配置接口,传递第三方平台的配置信息到对应的第三方组件,另外提供了公有的获取接口提供外部使用,这样来达到配置的解耦。PTThirdPlatformManager �这个管理配置的类相当于一个中间者,或者说是一个代理

结构图

结构图

接口部分

@protocol PTThirdPlatformConfigurable <NSObject>

/**
 *  设置平台的appkey
 *
 *  @param platformType 平台类型 @see PTThirdPlatformType
 *  @param appKey       第三方平台的appKey
 *  @param appID        第三方平台的appID
 *  @param appSecret    第三方平台的appSecret
 *  @param redirectURL  redirectURL
 */
- (BOOL)setPlaform:(PTThirdPlatformType)platformType
             appID:(NSString *)appID
            appKey:(NSString *)appKey
         appSecret:(NSString *)appSecret
       redirectURL:(NSString *)redirectURL;

- (NSString*)appIDWithPlaform:(PTThirdPlatformType)platformType;
- (NSString*)appKeyWithPlaform:(PTThirdPlatformType)platformType;
- (NSString*)appSecretWithPlaform:(PTThirdPlatformType)platformType;
- (NSString*)appRedirectURLWithPlaform:(PTThirdPlatformType)platformType;

实现部分

这部分的代码还是放在 PTThirdPlatformManager 这个类中,实现起来很简单,只是把不同平台的配置信息保存在一个字典中。

#pragma mark - ......::::::: PTThirdPlatformConfigurable Override :::::::......

- (BOOL)setPlaform:(PTThirdPlatformType)platformType
             appID:(NSString *)appID
            appKey:(NSString *)appKey
         appSecret:(NSString *)appSecret
       redirectURL:(NSString *)redirectURL {
    [self.thirdPlatformKeysConfig
     setObject:@(platformType)
     forKey:@{@(PTThirdPlatformAppID): ValueOrEmpty(appID),
              @(PTThirdPlatformAppKey): ValueOrEmpty(appKey),
              @(PTThirdPlatformAppSecret): ValueOrEmpty(appSecret),
              @(PTThirdPlatformRedirectURI): ValueOrEmpty(redirectURL)}];
    return YES;
}

- (NSString*)appIDWithPlaform:(PTThirdPlatformType)platformType {
    return [[self.thirdPlatformKeysConfig objectForKey:@(platformType)] objectForKey:@(PTThirdPlatformAppID)];
}

- (NSString*)appKeyWithPlaform:(PTThirdPlatformType)platformType {
    return [[self.thirdPlatformKeysConfig objectForKey:@(platformType)] objectForKey:@(PTThirdPlatformAppKey)];
}

- (NSString*)appSecretWithPlaform:(PTThirdPlatformType)platformType {
     return [[self.thirdPlatformKeysConfig objectForKey:@(platformType)] objectForKey:@(PTThirdPlatformAppSecret)];
}

- (NSString*)appRedirectURLWithPlaform:(PTThirdPlatformType)platformType {
    return [[self.thirdPlatformKeysConfig objectForKey:@(platformType)] objectForKey:@(PTThirdPlatformRedirectURI)];
}

具体的,不同平台配置APPID或者APPKey的地方是不统一
微博平台在 PTWeiboManager 中的配置:

- (void)thirdPlatConfigWithApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 子类实现
    // 初始化微博模块
#if DEBUG
    [WeiboSDK enableDebugMode:YES];
    DBLog(@"WeiboSDK getSDKVersion = %@", [WeiboSDK getSDKVersion]);
#endif
    NSString* appKey = [[PTThirdPlatformManager sharedInstance] appKeyWithPlaform:PTThirdPlatformTypeWeibo];
    [WeiboSDK registerApp:appKey];
}

腾讯QQ平台在 PTTencentRespManager 中的配置:

- (instancetype)init
{
    self = [super init];
    if (self) {
        NSString* appID = [[PTThirdPlatformManager sharedInstance] appIDWithPlaform:PTThirdPlatformTypeTencentQQ];
        _tencentOAuth = [[TencentOAuth alloc] initWithAppId:appID andDelegate:self];
    }
    return self;
}

- (TencentOAuth *)tencentOAuth {
    if (!_tencentOAuth) {
        NSString* appID = [[PTThirdPlatformManager sharedInstance] appIDWithPlaform:PTThirdPlatformTypeTencentQQ];
        _tencentOAuth = [[TencentOAuth alloc] initWithAppId:appID andDelegate:self];
    }
    return _tencentOAuth;
}

代码和目录结构

代码的简化
支付类型和登录类型合并为同一种类型,这两种类型在一个平台中只有一种方式,所以不用区分,以保持代码的简单

// 登录类型
typedef NS_ENUM(NSInteger, PTThirdPlatformType) {
    PTThirdPlatformTypeWechat = 1,//微信
    PTThirdPlatformTypeTencentQQ,//QQ
    PTThirdPlatformTypeWeibo,//微博
};

// 支付类型
typedef NS_ENUM(NSUInteger, PTPaymentMethodType) {
    PaymentMethodTypeWechat = 1,
    PaymentMethodTypeAlipay = 2,
    PaymentMethodTypeApple = 3,
};

修改为:

// 第三方平台类型
typedef NS_ENUM(NSInteger, PTThirdPlatformType) {
    PTThirdPlatformTypeWechat = 1,//微信
    PTThirdPlatformTypeTencentQQ,//QQ
    PTThirdPlatformTypeWeibo,//微博
    PTThirdPlatformTypeAlipay,//支付宝
};

对应的使用到的地方也要做相应的调整,这里就不再赘述了。

目录结构的调整:

目录结构的调整

左边是调整过后的目录结果,右边是调整之前的,主要是把类和接口进行了分离存放在对应的目录,此外吧 PTOrderModel 这个模型类也保存在了PTThirdPlatformObject类中

POD库

Pod私有库的创建有很多细节的东西,这里不会做详细的说明,只说明大概的做法和使用方法。

1、私有库的创建

➜  DevPods pod lib create PTThirdPlatformKit
Cloning `https://github.com/CocoaPods/pod-template.git` into `PTDataModule`.
Configuring PTDataModule template.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: 
 - http://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )


What language do you want to use?? [ Swift / ObjC ]
 > Objc

Would you like to include a demo application with your library? [ Yes / No ]
 > 
yes
Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > None

Would you like to do view based testing? [ Yes / No ]
 > No

What is your class prefix?
 > PT

Running pod install on your new library.

创建库的过程需要填写相应的信息,按照提示即可创建一个是有的Pod库了。

2、私有POD库的使用
Podfile文件中添加POD引用,因为我这里使用的是Pod的Example工程,所以指向的Pod库路径是上一层目录,其他的项目中使用的方式会有差别,这里需要注意下。

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

POD库配置系统库的依赖

使用POD库有个特性是可以配置依赖到的frameworkslibrariescompiler_flags ,在 podspec 文件中添加如下的配置,在 pod install 安装了pod库之后会配置好对应的依赖,而不用手动一个个的添加

    # 配置系统Framework
    s.frameworks = 'CoreMotion'

    # 添加依赖的系统静态库
    s.libraries = 'xml2', 'z', 'c++', 'stdc++.6', 'sqlite3'

子模块化第三方平台

pod 支持配置子模块,一般的把共用的代码放在Core模块中,把一系列相似的模块代码放在独立的子模块中单独管理,使用的时候可以通过配置podfile按需导入不同的平台。这样一方面模块化更清晰,另一方面不会导入多余的代码,导致包体积变大。
下面的配置文件配置了一个保存基础公有代码的 Core 子模块和四个不同平台的子模块,子模块中管理各自的源文件、依赖库、资源等信息,并且不同的子模块都不 Core 模块当做基础依赖。

s.default_subspec = 'Core'

    s.subspec 'Core' do |subspec|
        # 源代码
        subspec.source_files = 'PTThirdPlatformKit/Classes/**/*'
        # 配置系统Framework
        subspec.frameworks = 'CoreMotion'
        subspec.dependency 'SDWebImage'
        # 添加依赖的系统静态库
        subspec.libraries = 'xml2', 'z', 'c++', 'stdc++.6', 'sqlite3'
    end


    s.subspec 'AlipayManager' do |subspec|
        # 源代码
        subspec.source_files = 'PTThirdPlatformKit/AlipayManager/**/*'
        # 添加资源文件
        subspec.resource = 'PTThirdPlatformKit/AlipayManager/**/*.bundle'
        # 添加依赖的framework
        subspec.vendored_frameworks = 'PTThirdPlatformKit/AlipayManager/**/*.framework'
        subspec.frameworks = 'CoreTelephony', 'SystemConfiguration'
        subspec.dependency 'PTThirdPlatformKit/Core'
    end


    s.subspec 'TencentManager' do |subspec|
        # 源代码
        subspec.source_files = 'PTThirdPlatformKit/TencentManager/**/*'
        # 添加资源文件
        subspec.resource = 'PTThirdPlatformKit/TencentManager/**/*.bundle'
        # 添加依赖的framework
        subspec.vendored_frameworks = 'PTThirdPlatformKit/TencentManager/**/*.framework'
        subspec.frameworks = 'SystemConfiguration'
        subspec.dependency 'PTThirdPlatformKit/Core'
    end


    s.subspec 'WeiboManager' do |subspec|
        # 源代码
        subspec.source_files = 'PTThirdPlatformKit/WeiboManager/**/*'
        subspec.dependency 'WeiboSDK'
        subspec.dependency 'PTThirdPlatformKit/Core'
    end

    s.subspec 'WXManager' do |subspec|
        # 源代码
        subspec.source_files = 'PTThirdPlatformKit/WXManager/**/*'
        subspec.dependency 'WechatOpenSDK'
        subspec.dependency 'PTThirdPlatformKit/Core'
    end

podfile中配置需要导入的子模块,可以按需导入一个或者多个子模块

    pod 'PTThirdPlatformKit', :path => '../'
    pod 'PTThirdPlatformKit/AlipayManager', :path => '../'
    pod 'PTThirdPlatformKit/TencentManager', :path => '../'
    pod 'PTThirdPlatformKit/WeiboManager', :path => '../'
    pod 'PTThirdPlatformKit/WXManager', :path => '../'

插件化添加第三方平台

插件化的主要思路就是在原有功能和框架的基础上,提供一个扩展点,方便用户的扩展,用户可以实现框架中的接口或者父类实现自定义的功能扩展。
以当前项目为例,添加了两个扩展点:第三方平台登录或者支付的扩展点以及第三方分享的扩展点:

#pragma mark 插件接入点

/**
 插件接入点-添加登录或者是支付的管理类
 
 @param platformType 自定义的第三方平台类型,大于999
 @param managerClass 实现了PTAbsThirdPlatformManager接口的自定义第三方平台管理类
 */
- (void)addCustomPlatform:(NSInteger)platformType managerClass:(Class)managerClass {
    NSString* classString = NSStringFromClass(managerClass);
    if (classString) {
        [self.thirdPlatformManagerConfig setObject:NSStringFromClass(managerClass) forKey:@(platformType)];
        [self.thirdPlatformManagerClasses addObject:classString];
    }
}

/**
 插件接入点-添加分享的管理类
 
 @param sharePlatformType 自定义的第三方平台分享类型,大于999
 @param managerClass 实现了PTAbsThirdPlatformManager接口的自定义第三方平台管理类
 */
- (void)addCustomSharePlatform:(NSInteger)sharePlatformType managerClass:(Class)managerClass {
    NSString* classString = NSStringFromClass(managerClass);
    if (classString) {
        [self.thirdPlatformShareManagerConfig setObject:classString forKey:@(sharePlatformType)];
        [self.thirdPlatformManagerClasses addObject:classString];
    }
}

因为项目已经对模块进行了抽象化,每个模块都是抽象的具体事项,互不影响。高层的管理类依赖的是抽象的接口,而不直接依赖具体的实现,低层的具体模块依赖于模块的抽象接口,这就是所谓的** 依赖倒置 **。所以新增一个模块不过是添加一个新的配置而已,对已有的框架不会有任何的影响,添加新的模块更新已有的配置就能达到目的。

还有待改进

先到此结束,后面有改进再更新,有问题或者建议意见的欢迎评论我

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容