iOS 通过MOB平台集成第三方分享,自定义UI

一、前言

  前些年写了一篇类似的博客,老是有人来问,代码已经很老了,正好上周做了微信和支付宝的支付,所以今天花一点时间重新梳理下这块的代码,集成最新的MobSDK。

  为什么选择MOB平台?

  官网:https://www.mob.com/mobService/sharesdk

  答:对于移动客户端来说,第三方分享和登陆是基本功能,但是目前流行的三方APP太多了,微信、QQ、新浪微博、淘宝、易信、美拍、、、如果没集成一个特殊的分享就要去集成一个APP,对于开发者来说这种方式不友好,而MOB的作用就是它会集成基本所有的第三方平台,你在集成mob的时候可以选择性的选择想要集成的哪几个平台。

    而且,mob平台是支持双端的。

二、集成

  1、直接cocopods集成吧(我这边只需要微信和QQ的分享,而且项目里面如果集成了微信支付的话,要选择简略版的微信集成)

# 主模块(必须)
  pod 'mob_sharesdk'
  pod 'mob_sharesdk/ShareSDKExtension'
  pod 'mob_sharesdk/ShareSDKPlatforms/QQ'
  pod 'mob_sharesdk/ShareSDKPlatforms/WeChat_Lite'

  注:我这里只集成了微信和QQ的分享模块,对于其他模块自己选择

# 平台SDK模块其他平台,按需添加
pod 'mob_sharesdk/ShareSDKPlatforms/RenRen'
pod 'mob_sharesdk/ShareSDKPlatforms/AliPaySocial'
pod 'mob_sharesdk/ShareSDKPlatforms/Kakao'
pod 'mob_sharesdk/ShareSDKPlatforms/Yixin'
pod 'mob_sharesdk/ShareSDKPlatforms/Facebook'
pod 'mob_sharesdk/ShareSDKPlatforms/Messenger'
pod 'mob_sharesdk/ShareSDKPlatforms/Copy'
pod 'mob_sharesdk/ShareSDKPlatforms/Evernote'
pod 'mob_sharesdk/ShareSDKPlatforms/GooglePlus'
pod 'mob_sharesdk/ShareSDKPlatforms/Instagram'
pod 'mob_sharesdk/ShareSDKPlatforms/Instapaper'
pod 'mob_sharesdk/ShareSDKPlatforms/Line'
pod 'mob_sharesdk/ShareSDKPlatforms/Mail'
pod 'mob_sharesdk/ShareSDKPlatforms/SMS'
pod 'mob_sharesdk/ShareSDKPlatforms/WhatsApp'
pod 'mob_sharesdk/ShareSDKPlatforms/MeiPai'
pod 'mob_sharesdk/ShareSDKPlatforms/DingTalk'
pod 'mob_sharesdk/ShareSDKPlatforms/YouTube'
pod 'mob_sharesdk/ShareSDKPlatforms/Twitter'
pod 'mob_sharesdk/ShareSDKPlatforms/Dropbox'
pod 'mob_sharesdk/ShareSDKPlatforms/DingTalk'
pod 'mob_sharesdk/ShareSDKPlatforms/CMCC'
pod 'mob_sharesdk/ShareSDKPlatforms/Telegram'
pod 'mob_sharesdk/ShareSDKPlatforms/Reddit'
pod 'mob_sharesdk/ShareSDKPlatforms/DouBan'
pod 'mob_sharesdk/ShareSDKPlatforms/Flickr'
pod 'mob_sharesdk/ShareSDKPlatforms/KaiXin'
pod 'mob_sharesdk/ShareSDKPlatforms/LinkedIn'
pod 'mob_sharesdk/ShareSDKPlatforms/MingDao'
pod 'mob_sharesdk/ShareSDKPlatforms/Pinterest'
pod 'mob_sharesdk/ShareSDKPlatforms/Pocket'
pod 'mob_sharesdk/ShareSDKPlatforms/Print'
pod 'mob_sharesdk/ShareSDKPlatforms/TencentWeibo'
pod 'mob_sharesdk/ShareSDKPlatforms/Tumblr'
pod 'mob_sharesdk/ShareSDKPlatforms/VKontakte'
pod 'mob_sharesdk/ShareSDKPlatforms/YouDaoNote'
pod 'mob_sharesdk/ShareSDKPlatforms/ESurfing'
pod 'mob_sharesdk/ShareSDKPlatforms/FacebookAccount'

  2、注册

  2.1、首先在plist文件里面添加两个参数,MOBAppkey 和 MOBAppSecret 两个字段。

 

  2.2、在didFinishLaunchingWithOptions方法里面注册相关 的平台

//share分享注册
    [ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {//注册QQ,QQ空间
        [platformsRegister setupQQWithAppId:@"QQID" appkey:@"key"];
    }];
    
    [ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {//注册微信,朋友圈
        [platformsRegister setupWeChatWithAppId:WeiXinPayKey appSecret:@"key" universalLink:WeiXinLinks];
    }];

  2.3、在info --> URL Types 里面添加schemes,以微信和QQ为例,QQ又分为QQ会话和QQ空间,QQ的AppID,其格式为:”QQ” + AppId的16进制(如果appId转换的16进制数不够8位则在前面补0,如转换的是:5FB8B52,则最终填入为:QQ05FB8B52 注意:转换后的字母要大写) 转换16进制的方法:echo ‘ibase=10;obase=16;801312852’ |bc,其中801312852为QQ的AppID

 

  注:这块是有点麻烦的,要细心点

  2.4、配置白名单

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>wechat</string>
        <string>weixin</string>
        <string>weixinULAPI</string>
        <string>sinaweibohd</string>
        <string>sinaweibo</string>
        <string>sinaweibosso</string>
        <string>weibosdk</string>
        <string>weibosdk2.5</string>
        <string>mqqapi</string>
        <string>mqq</string>
        <string>mqqOpensdkSSoLogin</string>
        <string>mqqconnect</string>
        <string>mqqopensdkdataline</string>
        <string>mqqopensdkgrouptribeshare</string>
        <string>mqqopensdkfriend</string>
        <string>mqqopensdkapi</string>
        <string>mqqopensdkapiV2</string>
        <string>mqqopensdkapiV3</string>
        <string>mqqopensdkapiV4</string>
        <string>mqzoneopensdk</string>
        <string>wtloginmqq</string>
        <string>wtloginmqq2</string>
        <string>mqqwpa</string>
        <string>mqzone</string>
        <string>mqzonev2</string>
        <string>mqzoneshare</string>
        <string>wtloginqzone</string>
        <string>mqzonewx</string>
        <string>mqzoneopensdkapiV2</string>
        <string>mqzoneopensdkapi19</string>
        <string>mqzoneopensdkapi</string>
        <string>mqqbrowser</string>
        <string>mttbrowser</string>
        <string>fbapi</string>
        <string>fb-messenger-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
        <string>twitter</string>
        <string>twitterauth</string>
        <string>JChatPro</string>
    </array>

  注:直接放到plist文件里面就好。

  3、调用

  由于我们这边采用的是自定义UI,所以调用放大有所差异,直接上代码吧

//点击分享
- (void)userClickShareBtnAction {
    BYShareALertView *shareView = [[BYShareALertView alloc] initWithAlert];
    [shareView showAlert];
    NSMutableDictionary *parameters = [NSMutableDictionary new];
    NSString *byText = :@"分享内容";
    shareView.clickShareBtnBlock = ^(NSInteger tag) {
        switch (tag) {
            case 100:
                //微信
                [parameters SSDKSetupShareParamsByText:byText
                                                images:@[[UIImage imageNamed:@"Blink"]]
                                                   url:[NSURL URLWithString:@"www.baidu.com"]
                                                 title:@"分享标题"
                                                  type:SSDKContentTypeWebPage];
                [self shareWithParameters:parameters withShareType:SSDKPlatformSubTypeWechatSession];

                break;
            case 101:
                //微信朋友圈
                [parameters SSDKSetupShareParamsByText:byText
                                                images:@[[UIImage imageNamed:@"Blink"]]
                                                   url:[NSURL URLWithString:@"www.baidu.com"]
                                                 title:@"分享标题"
                                                  type:SSDKContentTypeWebPage];
                [self shareWithParameters:parameters withShareType:SSDKPlatformSubTypeWechatTimeline];

                break;
            case 102:
                //QQ
                [parameters SSDKSetupShareParamsByText:byText
                                                images:@[[UIImage imageNamed:@"APPicon"]]
                                                   url:[NSURL URLWithString:@"www.baidu.com"]
                                                 title:@"分享标题"
                                                  type:SSDKContentTypeWebPage];
                [self shareWithParameters:parameters withShareType:SSDKPlatformSubTypeQQFriend];

                break;
            case 103:
                //QQ空间
                [parameters SSDKSetupShareParamsByText:byText
                                                images:@[[UIImage imageNamed:@"APPicon"]]
                                                   url:[NSURL URLWithString:@"http://www.baidu.com"]
                                                 title:@"分享标题"
                                                  type:SSDKContentTypeWebPage];
                [self shareWithParameters:parameters withShareType:SSDKPlatformSubTypeQZone];

                break;
            default:
                break;
        }
    };
}

- (void)shareWithParameters:(NSMutableDictionary *)dict withShareType:(SSDKPlatformType)type {
    [ShareSDK share:type parameters:dict onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        if (state == SSDKResponseStateSuccess) {
            [kKeyWindow makeToast:@"分享成功"];
        } else if (state == SSDKResponseStateFail) {
            [kKeyWindow makeToast:@"未检测到客户端 分享失败"];
        } else if (state == SSDKResponseStateCancel) {
            [kKeyWindow makeToast:@"分享已取消"];
        }
    }];

}

  注:如遇到分享失败的情况,请第一时间检查来自QQ和微信的APPID、Key这些参数是否正确。

三、总结

  总的来说还是蛮简单 的,关键是QQ与微信的相关Key不好申请。

  下次再来集成下第三方登陆吧,其实大同小异,调用不同的方法,获取头像与昵称罢了。

 

posted on 2019-12-01 19:30  麦芽呀~  阅读(1243)  评论(0编辑  收藏  举报