架构江湖-第三方支付分享登录SDK设计(oc)

在我们项目开发中集成第三方的支付,分享,登录功能必不可少,如果本文也只是用代理设计模式怼了这几个功能,是不是有点对不起各位看官了。
剧透:

  • 远离我们的AppDelegate,将繁杂的回调处理扔出去
  • 分平台。例如:微信类只实现微信的分享,登录,支付等等,对其他平台不干扰
  • 便捷的增删某个平台,增删某个平台内的功能,

先看下我们的Demo实现了哪些功能,类似于简易版的友盟或者ShareSDK,有点扯淡了


架构江湖-第三方支付分享登录SDK设计(oc)_第1张图片
第三方实现.png

每个小图标的功能都进行了实现


架构江湖-第三方支付分享登录SDK设计(oc)_第2张图片
界面展示

工程的目录结构,很清晰

架构江湖-第三方支付分享登录SDK设计(oc)_第3张图片
image.png

惨了没话说了, 来个Demo压压惊

来到AppDelegate进行瘦身

/** iOS9.0以下回调 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [[SharePayContext shareContext] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}

/** iOS9.0以上版本请实现如下方法。 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:  (NSDictionary *)options{
    
    return [[SharePayContext shareContext] application:application openURL:url options:options];
}

AppDelegate需要做的也就这么多了

上下文管理者,对各个平台的功能进行管理和分发

首先我们需要明确功能:登录,分享,支付和回调处理
分析他们的共性和差异化,写出对应的方法参数

+ (instancetype)shareContext;

/** 第三方登录 */
- (void)loginPlatform:(SharePayChannel)loginplatform withComplation:(SharePayComplationBlcok)complation;

/** 分享 */
- (void)sharingplatform:(SharePayChannel)sharingplatform title:(NSString *)title message:(NSString *)message imgUrl:(NSString *)imgUrl shareUrl:(NSString *)shareUrl controller:(UIViewController*)controller withComplation:(SharePayComplationBlcok)complation;

/** 唤醒支付 */
- (void)payWithCharge:(NSDictionary*)chargeInfo channel:(SharePayChannel)payChannel controller:(UIViewController*)controller scheme:(NSString*)scheme withComplation:(SharePayComplationBlcok)complation;

/** 9.0以前回调 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;

/** 9.0以后回调 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:  (NSDictionary *)options;

如何进行管理和分发呢?
在管理类中我们需要创建各个平台的实现类,然后通过他们来访问内部的具体功能。

每个平台功能的实现是不一样的,那该怎么办?
定义共同协议接口,也就是让他们穿上一样的外衣,管理者进行任务派发时看见相同的衣服就知道是自家人。

每个平台的功能功能怎么实现?
遵守协议,在对应的协议方法下实现对应的功能?

如何增删平台,增删某个平台内的功能?
创建类>>>遵守协议>>>实现指定功能下的协议方法>>>不需要这个功能就不实现这个协议方法

定义的协议接口

/** 处理结果回调 */
typedef void (^SharePayComplationBlcok)(NSString* result,SharePayError *error);

/** 支付渠道 */
typedef NS_ENUM(NSUInteger,SharePayChannel){
    
    /** 微信 */
    SharePay_WeChatChannel = 1,
    
    SharePay_WeChatFriendCircleChannel,
    
    /** 腾讯*/
    SharePay_TencentQQChannel,
    
    SharePay_TencentQQZoneChannel,
    
    /** 支付宝 */
    SharePay_AliPayChannel,
    
    /** 新浪 */
    SharePay_SinaChannel,
    
    /** Copy */
    SharePay_CopyChannel,
};

@protocol SharePayProtocol 

@optional

/** 9.0以前回调 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;

/** 9.0以后回调 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:  (NSDictionary *)options;

/** 登录认证 */
- (void)loginAuthRequestComplation:(SharePayComplationBlcok)complation;

/** 分享 */
- (void)shareTitle:(NSString *)title message:(NSString *)message imgUrl:(NSString *)imgUrl shareUrl:(NSString *)shareUrl controller:(UIViewController*)controller isCircleFriends:(BOOL)circleFriends isQQZone:(BOOL)qqZone withComplation:(SharePayComplationBlcok)complation;

/** 唤醒支付 */
- (void)payWithCharge:(NSDictionary*)chargeInfo controller:(UIViewController*)controller scheme:(NSString*)scheme withComplation:(SharePayComplationBlcok)complation;

@end

看看我们的管理者都做了些什么

  • 创建实例对象>>>
  • 使用异常处理类判断参数的有效性>>>
  • 判断是否实现了协议方法>>>
  • 访问该方法>>>
  • Gameover
/** 第三方登录 */
- (void)loginPlatform:(SharePayChannel)loginplatform withComplation:(SharePayComplationBlcok)complation
{
    /** 渠道验证 */
    if (![SharePayErrorUtility invalidChannel:loginplatform withComplation:complation]) {
        return;
    }
    
    NSString *channelType = [NSString stringWithFormat:@"%lu",loginplatform];
    NSString *className = _channelDic[channelType];
    _sharepaySate = [[NSClassFromString(className) alloc] init];
    
    if ([_sharepaySate respondsToSelector:@selector(loginAuthRequestComplation:)]) {
        [_sharepaySate loginAuthRequestComplation:complation];
    }
}

/** 分享 */
- (void)sharingplatform:(SharePayChannel)sharingplatform title:(NSString *)title message:(NSString *)message imgUrl:(NSString *)imgUrl shareUrl:(NSString *)shareUrl controller:(UIViewController*)controller withComplation:(SharePayComplationBlcok)complation;
{
    /** 渠道验证 */
    if (![SharePayErrorUtility invalidChannel:sharingplatform withComplation:complation]) {
        return;
    }
    
    BOOL isQQZone = NO;
    BOOL isCircleFriend = NO;
    
    if (sharingplatform == SharePay_WeChatFriendCircleChannel ) {
        
        isCircleFriend = YES;
    }else if (sharingplatform == SharePay_TencentQQZoneChannel) {
        
        isQQZone = YES;
    }
    
    NSString *channelType = [NSString stringWithFormat:@"%lu",sharingplatform];
    NSString *className = _channelDic[channelType];
    _sharepaySate = [[NSClassFromString(className) alloc] init];
    
    if ([_sharepaySate respondsToSelector:@selector(shareTitle:message:imgUrl:shareUrl:controller:isCircleFriends:isQQZone:withComplation:)]) {
        [_sharepaySate shareTitle:title message:message imgUrl:message shareUrl:shareUrl controller:controller isCircleFriends:isCircleFriend isQQZone:isQQZone withComplation:complation];
    }
}

/** 1、唤醒支付 */
- (void)payWithCharge:(NSDictionary*)chargeInfo channel:(SharePayChannel)payChannel controller:(UIViewController*)controller scheme:(NSString*)scheme withComplation:(SharePayComplationBlcok)complation
{
    /** 新增支付渠道需要 用到controller 是需验证是否为空 */
    if (![SharePayErrorUtility invalidCharge:chargeInfo withComplation:complation]) {
        return;
    }
    
    /** 渠道验证 */
    if (![SharePayErrorUtility invalidChannel:payChannel withComplation:complation]) {
        return;
    }
    
    NSString *channelType = [NSString stringWithFormat:@"%lu",payChannel];
    NSString *className = _channelDic[channelType];
    _sharepaySate = [[NSClassFromString(className) alloc] init];
    
    if ([_sharepaySate respondsToSelector:@selector(payWithCharge:controller:scheme:withComplation:)]) {
        [_sharepaySate payWithCharge:chargeInfo controller:controller scheme:scheme withComplation:complation];
    }
}

/** 2、9.0以前回调 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [_sharepaySate application:application openURL:url sourceApplication:sourceApplication annotation:application];
}

/** 3、9.0以后回调 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:  (NSDictionary *)options
{
    return [_sharepaySate application:application openURL:url options:options];
}

白名单备份

配置plist文件
 LSApplicationQueriesSchemes
    
        wechat
        weixin
        sinaweibohd
        sinaweibo
        sinaweibosso
        weibosdk
        mqqapi
        mqq
        mqqOpensdkSSoLogin
        mqqconnect
        mqqopensdkdataline
        mqqopensdkgrouptribeshare
        mqqopensdkfriend
        mqqopensdkapi
        mqqopensdkapiV2
        mqqopensdkapiV3
        mqzoneopensdk
        wtloginmqq
        wtloginmqq2
        mqqwpa
        mqzone
        mqzonev2
        mqzoneshare
        wtloginqzone
        mqzonewx
        mqzoneopensdkapiV2
        mqzoneopensdkapi19
        mqzoneopensdkapi
        mqzoneopensdk
        alipay
        alipayshare
    
    NSAppTransportSecurity
    
        NSAllowsArbitraryLoads
        
    

Demo源码

你可能感兴趣的:(架构江湖-第三方支付分享登录SDK设计(oc))