美文网首页
CocoaPods集成ShareSDK(标准版)

CocoaPods集成ShareSDK(标准版)

作者: March_Cullen | 来源:发表于2017-03-06 15:11 被阅读0次

    步骤:

    • 1、获取AppKey

    • 2、通过CocoaPods安装

    • 3、快速集成

      • 3.1 设置ShareSDK的AppKey
      • 3.2 添加实现代码
      • 3.3 支持微信所需的相关配置及代码
      • 3.4 支持QQ所需的相关配置及代码
      • 3.5 配置新浪微博、Facebook的SSO
      • 3.6 iPad版本分享菜单继承
    • 1、获取AppKey

    • 2、通过CocoaPods安装
      通过CocoaPods安装ShareSDK,只需在Podfile文件中添加

    pod "ShareSDK2"
    pod "MOBFoundation"
    
    # UI模块(含所有UI样式,可选)
    pod 'ShareSDK2/UI'
    # iOS竖版默认分享UI(可选)
    pod 'ShareSDK2/UI/Flat'
    # iPad版默认分享视图(可选)
    pod 'ShareSDK2/UI/iPadDefault'
    # iPad版简单分享视图(可选)
    pod 'ShareSDK2/UI/iPadSimple'
    # iPhone版默认分享视图(可选)
    pod 'ShareSDK2/UI/iPhoneDefault'
    # iPhone版简单分享视图(可选)
    pod 'ShareSDK2/UI/iPhoneSimple'
    # iPhone版应用推荐视图(可选)
    pod 'ShareSDK2/UI/iPhoneAppRecommend'
    
    # 评论和赞模块(可选)
    pod 'ShareSDK2/Comment'
    # 分享&登录链接模块(含所有平台,可选)
    pod 'ShareSDK2/Connection'
    # 短信(可选)
    pod 'ShareSDK2/Connection/SMS'
    # 邮件(可选)
    pod 'ShareSDK2/Connection/Mail'
    # 拷贝(可选)
    pod 'ShareSDK2/Connection/Copy'
    # 打印(可选)
    pod 'ShareSDK2/Connection/Print'
    # 新浪微博(可选)
    pod 'ShareSDK2/Connection/SinaWeibo'
    # 微信(可选)
    pod 'ShareSDK2/Connection/WeChat'
    # 腾讯QQ(可选)
    pod 'ShareSDK2/Connection/QQ'
    # QQ空间(可选)
    pod 'ShareSDK2/Connection/QZone'
    # 腾讯微博(可选)
    pod 'ShareSDK2/Connection/TencentWeibo'
    # Google+(可选)
    pod 'ShareSDK2/Connection/GooglePlus'
    # 人人网(可选)
    pod 'ShareSDK2/Connection/RenRen'
    # 易信(可选)
    pod 'ShareSDK2/Connection/YiXin'
    # Facebook(可选)
    pod 'ShareSDK2/Connection/Facebook'
    # Dropbox(可选)
    pod 'ShareSDK2/Connection/Dropbox'
    # DouBan(可选)
    pod 'ShareSDK2/Connection/DouBan'
    # 印象笔记(可选)
    pod 'ShareSDK2/Connection/EverNote'
    # Flickr(可选)
    pod 'ShareSDK2/Connection/Flickr'
    # Instagram(可选)
    pod 'ShareSDK2/Connection/Instagram'
    # Instapaper(可选)
    pod 'ShareSDK2/Connection/Instapaper'
    # 开心网(可选)
    pod 'ShareSDK2/Connection/KaiXin'
    # Twitter(可选)
    pod 'ShareSDK2/Connection/Twitter'
    # Tumblr(可选)
    pod 'ShareSDK2/Connection/Tumblr'
    # WhatsApp(可选)
    pod 'ShareSDK2/Connection/WhatsApp'
    # VKontakte(可选)
    pod 'ShareSDK2/Connection/VKontakte'
    # KaKaoStory(可选)
    pod 'ShareSDK2/Connection/KaKaoStory'
    # KaKaoTalk(可选)
    pod 'ShareSDK2/Connection/KaKaoTalk'
    # Line(可选)
    pod 'ShareSDK2/Connection/Line'
    # LinkedIn(可选)
    pod 'ShareSDK2/Connection/LinkedIn'
    # Pocket(可选)
    pod 'ShareSDK2/Connection/Pocket'
    # 明道(可选)
    pod 'ShareSDK2/Connection/MingDao'
    # 有道云笔记(可选)
    pod 'ShareSDK2/Connection/YouDaoNote'
    # 支付宝(可选)
    pod 'ShareSDK2/Connection/AliPaySocial'
    

    安装ShareSDK2/UI 模块可使用所有UI界面,安装ShareSDK2/Connection 模块可使用所有分享平台。但鉴于安装所有分享平台模块会使得应用变得非常庞大,所以不推荐大家使用这种方式安装。开发者可根据自己的需求安装引入指定的分享模块,这样可使应用体积保持小巧。
    例:应用需要包含新浪微博分享,微信和 QQ 分享,并且需要使用简单分享视图,则只需要添加如下代码于 Podfile 中进行安装即可。

    pod 'ShareSDK2'
    # Mob 公共库(必须) 如果同时集成SMSSDK iOS2.0:可看此注意事项:http://bbs.mob.c om/thread-20051-1-1.html
    pod 'MOBFoundation'
     
    pod 'ShareSDK2/UI/Flat'
    pod 'ShareSDK2/UI/iPadDefault'
     
    pod 'ShareSDK2/Connection/SinaWeibo'
    pod 'ShareSDK2/Connection/WeChat'
    pod 'ShareSDK2/Connection/QQ'
    
    • 3、快速集成
      • 3.1 设置ShareSDK的AppKey

    打开AppDelegate.m(代表你的工程名字)导入文件头ShareSDK.h

    #import <ShareSDK/ShareSDK.h>
    #import <TencentOpenAPI/QQApiInterface.h>
    #import <TencentOpenAPI/TencentOAuth.h>
    #import "WXApi.h"
    #import "WeiboSDK.h"
    #import <RennSDK/RennSDK.h>
    

    - (BOOL)application: didFinishLaunchingWithOptions:
    

    方法中调用registerApp方法来初始化SDK并且初始化第三方平台

     [ShareSDK registerApp:@"api20"];//字符串api20为您的ShareSDK的AppKey
     
        //添加新浪微博应用 注册网址 http://open.weibo.com
        [ShareSDK connectSinaWeiboWithAppKey:@"568898243"
                                 appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                                 redirectUri:@"http://www.sharesdk.cn"];
        //当使用新浪微博客户端分享的时候需要按照下面的方法来初始化新浪的平台
        [ShareSDK  connectSinaWeiboWithAppKey:@"568898243"     
                                 appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3" 
                                 redirectUri:@"http://www.sharesdk.cn" 
                                 weiboSDKCls:[WeiboSDK class]];
     
        //添加腾讯微博应用 注册网址 http://dev.t.qq.com
        [ShareSDK connectTencentWeiboWithAppKey:@"801307650"
                                      appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
                                    redirectUri:@"http://www.sharesdk.cn"];
     
        //添加QQ空间应用  注册网址  http://connect.qq.com/intro/login/
        [ShareSDK connectQZoneWithAppKey:@"100371282"
                               appSecret:@"aed9b0303e3ed1e27bae87c33761161d"
                       qqApiInterfaceCls:[QQApiInterface class]
                         tencentOAuthCls:[TencentOAuth class]];
     
        //添加QQ应用  注册网址   http://mobile.qq.com/api/
        [ShareSDK connectQQWithQZoneAppKey:@"100371282"
                         qqApiInterfaceCls:[QQApiInterface class]
                           tencentOAuthCls:[TencentOAuth class]];
     
        //微信登陆的时候需要初始化
        [ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885"
                               appSecret:@"64020361b8ec4c99936c0e3999a9f249"
                               wechatCls:[WXApi class]];
     
        //添加豆瓣应用  注册网址 http://developers.douban.com
        [ShareSDK connectDoubanWithAppKey:@"07d08fbfc1210e931771af3f43632bb9"
                                 appSecret:@"e32896161e72be91"
                                 redirectUri:@"http://dev.kumoway.com/braininference/infos.php"];
     
        //添加人人网应用 注册网址  http://dev.renren.com
        [ShareSDK connectRenRenWithAppId:@"226427"
                                  appKey:@"fc5b8aed373c4c27a05b712acba0f8c3"
                               appSecret:@"f29df781abdd4f49beca5a2194676ca4"
                       renrenClientClass:[RennClient class]];
    //添加Instapaper应用   注册网址  http://www.instapaper.com/main/request_oauth_consumer_token
        [ShareSDK connectInstapaperWithAppKey:@"4rDJORmcOcSAZL1YpqGHRI605xUvrLbOhkJ07yO0wWrYrc61FA"
                                 appSecret:@"GNr1GespOQbrm8nvd7rlUsyRQsIo3boIbMguAl9gfpdL0aKZWe"];
     
        //添加Facebook应用  注册网址 https://developers.facebook.com
        [ShareSDK connectFacebookWithAppKey:@"107704292745179"
                                 appSecret:@"38053202e1a5fe26c80c753071f0b573"];
     
        //添加Twitter应用  注册网址  https://dev.twitter.com
        [ShareSDK connectTwitterWithConsumerKey:@"mnTGqtXk0TYMXYTN7qUxg"
                                 consumerSecret:@"ROkFqr8c3m1HXqS3rm3TJ0WkAJuwBOSaWhPbZ9Ojuc"
                                 redirectUri:@"http://www.sharesdk.cn"];
        //连接短信分享
        [ShareSDK connectSMS];
        //连接邮件
        [ShareSDK connectMail];
        //连接打印
        [ShareSDK connectAirPrint];
        //连接拷贝
        [ShareSDK connectCopy];
    
    • 3、快速集成
      • 3.2 添加实现代码

    打开需要继承分享功能的视图源码,把如下代码复制并粘贴到你将弹出一个分享列表的位置,例如到响应分享按钮的方法中。并且修改相应的参数即可。

    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK" ofType:@"png"];
     
        //构造分享内容
        id<ISSContent> publishContent = [ShareSDK content:@"分享内容"
                                           defaultContent:@"测试一下"
                                                    image:[ShareSDK imageWithPath:imagePath]
                                                    title:@"ShareSDK"
                                                      url:@"http://www.mob.com"
                                              description:@"这是一条测试信息"
                                                mediaType:SSPublishContentMediaTypeNews];
        //创建iPad弹出菜单容器,详见第六步
        id<ISSContainer> container = [ShareSDK container];
        [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
     //弹出分享菜单
        [ShareSDK showShareActionSheet:container
                             shareList:nil
                               content:publishContent
                         statusBarTips:YES
                           authOptions:nil
                          shareOptions:nil
                                result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
     
                                    if (state == SSResponseStateSuccess)
                                    {
                                        NSLog(@"分享成功");
                                    }
                                    else if (state == SSResponseStateFail)
                                    {
                                        NSLog(@"分享失败,错误码:%ld,错误描述:%@", [error errorCode], [error errorDescription]);
                                    }
                                }];
    

    运行效果如下:

    cocoapodsShareSDK_image0.png cocoapodsShareSDK_image1.png

    编译并运行后,弹出类似上图的分享列表选择页面,点击后弹出分享窗口,点击发表后就成功发送到社交平台的话说明你已经基本集成成功了。但是,为了使您的用户有更好的分享体验,以及您的应用有更多的分享量,更好地在社交平台上传播,我们建议您按照详“进阶功能”的指引进行SSO(免登录)和更多社交平台的设置。

    • 3、快速集成
      • 3.3 支持微信所需的相关配置及代码

    在微信开放平台(http://open.weixin.qq.com/ )注册应用并取得应用的AppID,然后打开下图位置,在URL Types中添加微信AppID

    cocoapodsShareSDK_image2.png

    打开AppDelegate.m(代表你的工程名字)文件,导入微信SDK的头文件

    #import "WXApi.h"
    

    - application: didFinishLaunchingWithOptions:
    

    方法内添加如下语句:

    [ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885"   //微信APPID
                            appSecret:@"64020361b8ec4c99936c0e3999a9f249"  //微信APPSecret
                            wechatCls:[WXApi class]];
    

    检查是否已加入handleOpenURL的处理方法,如果没有则添加如下代码:

    - (BOOL)application:(UIApplication *)application
           handleOpenURL:(NSURL *)url
    {
        return [ShareSDK handleOpenURL:url
                            wxDelegate:self];
    }
     
    - (BOOL)application:(UIApplication *)application
                 openURL:(NSURL *)url
       sourceApplication:(NSString *)sourceApplication
              annotation:(id)annotation
    {
        return [ShareSDK handleOpenURL:url
                     sourceApplication:sourceApplication
                           annotation:annotation
                            wxDelegate:self];
    }
    
    • 3、快速集成
      • 3.4 支持QQ所需的相关配置及代码

    登录QQ互联(http://connect.qq.com/ )注册成为开发者并登记应用取得AppId,然后打开下图位置,在URL Types中添加QQ的AppID,其格式为:”QQ” + AppId的16进制(如果appId转换的16进制数不够8位则在前面补0,如转换的是:5FB8B52,则最终填入为:QQ05FB8B52 注意:转换后的字母要大写)
    转换16进制的方法:echo ‘ibase=10;obase=16;801312852′|bc,其中801312852为QQ的AppID,如下

    cocoapodsShareSDK_image3.png cocoapodsShareSDK_image4.png

    然后打开工程中的AppDelegate.m(代表你的工程名字)文件,导入QQSDK的头文件:

    #import <TencentOpenAPI/QQApiInterface.h>
    #import <TencentOpenAPI/TencentOAuth.h>
    

    然后打开工程中的AppDelegate.m(代表你的工程名字)文件,在- (BOOL)application: didFinishLaunchingWithOptions:方法内添加如下语句:

    [ShareSDK connectQQWithQZoneAppKey:@"801312852"
                      qqApiInterfaceCls:[QQApiInterface class]
                        tencentOAuthCls:[TencentOAuth class]];
    

    检查是否已加入handleOpenURL的处理方法,如果没有则添加如下代码:

    - (BOOL)application:(UIApplication *)application
           handleOpenURL:(NSURL *)url
    {
        return [ShareSDK handleOpenURL:url
                            wxDelegate:self];
    }
     
    - (BOOL)application:(UIApplication *)application
                 openURL:(NSURL *)url
       sourceApplication:(NSString *)sourceApplication
              annotation:(id)annotation
    {
        return [ShareSDK handleOpenURL:url
                     sourceApplication:sourceApplication
                            annotation:annotation
                            wxDelegate:self];
    }
    
    • 3、快速集成
      • 3.5 配置新浪微博、Facebook的SSO

    登录新浪微博和facebook的开放平台,获取AppID 。新浪微博的url scheme的设置格式为wb+AppID,facebook的设置格式为fb+AppID。具体参考下图:

    cocoapodsShareSDK_image5.png

    - application: didFinishLaunchingWithOptions: 
    

    方法内添加如下语句:

      /**
         连接新浪微博开放平台应用以使用相关功能,此应用需要引用SinaWeiboConnection.framework
         http://open.weibo.com上注册新浪微博开放平台应用,并将相关信息填写到以下字段
         **/
        [ShareSDK connectSinaWeiboWithAppKey:@"568898243"
                                   appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                                 redirectUri:@"http://www.sharesdk.cn"];
        /**
         连接Facebook应用以使用相关功能,此应用需要引用FacebookConnection.framework
         https://developers.facebook.com上注册应用,并将相关信息填写到以下字段
         **/
        [ShareSDK connectFacebookWithAppKey:@"107704292745179"
                                  appSecret:@"38053202e1a5fe26c80c753071f0b573"];
    
    • 3、快速集成
      • 3.6 iPad版本分享菜单集成

    如果需要在iPad中显示分享,则需要使用+ container;功能接口创建一个容器对象并调用容器对象的setIPadContainer系列方法来设置iPad版显示容器。 一般而言,我们会建议使用一个比较小的、作为当前viewController 所控制的view 的subview 来作为“锚点”,来设置 container。例如一个已有的button。这样分享界面或者授权界面才能正确显示。调用接口如下:

    //创建弹出菜单容器
    id<ISSContainer> container = [ShareSDK container];
    [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDire ctionUp];
    //然后将container对象传入showShareActionSheet的第一个参数中,如: 
     
      //弹出分享菜单
      [ShareSDK showShareActionSheet:container
                           shareList:nil
                             content:publishContent
                           statusBarTips:YES
                             authOptions:nil
                             shareOptions:nil
                             result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                    if (state == SSResponseStateSuccess)
                                    {
                                        NSLog(@"分享成功");
                                    }
                                    else if (state == SSResponseStateFail)
                                    {
                                        NSLog(@"分享失败,错误码:%d,错误描述:%@", [error errorCode], [error errorDescription]);
                                    }
                                }];
    

    相关文章

      网友评论

          本文标题:CocoaPods集成ShareSDK(标准版)

          本文链接:https://www.haomeiwen.com/subject/gohugttx.html