IOS集成ShareSDK社会化分享

来源:互联网 发布:mac上steam游戏装在哪 编辑:程序博客网 时间:2024/05/01 16:32

最近在应用中加入了分享功能,使用了ShareSDK v3.x,以下纪录了在工程中集成ShareSDK社会化分享的步骤。

  • IOS版本:9.3
  • 开发环境:Xcode 7.3
  • ShareSDK版本:v3.x
ShareSDK官网上提供了快速集成的主要步骤,链接如下:点击打开链接,不详细说了。因为项目里只需要分享到QQ、微信和新浪微博平台,所以只加入对应的头文件,导入相应的库,将不需要的删除了。下面是ShareSDK初始化配置和分享界面及分享动作部分代码。

初始化代码,其中的AppID、AppKey、appSecret要替换为自己的,这些是在需要分享的平台上申请得到的。在activePlatforms中微信平台没有使用SSDKPlatformTypeWechat,因为SSDKPlatformTypeWechat默认包括微信好友、朋友圈和微信收藏,因为不需要收藏,所以用了子选项。

/* 初始化代码 */
-(void) shareSDKInit{    /**     *  设置ShareSDK的appKey,如果尚未在ShareSDK官网注册过App,请移步到http://mob.com/login 登录后台进行应用注册,     *  在将生成的AppKey传入到此方法中。我们Demo提供的appKey为内部测试使用,可能会修改配置信息,请不要使用。     *  方法中的第二个参数用于指定要使用哪些社交平台,以数组形式传入。第三个参数为需要连接社交平台SDK时触发,     *  在此事件中写入连接代码。第四个参数则为配置本地社交平台时触发,根据返回的平台类型来配置平台信息。     *  如果您使用的时服务端托管平台信息时,第二、四项参数可以传入nil,第三项参数则根据服务端托管平台来决定要连接的社交SDK。     */    [ShareSDK registerApp:@"bundle id"          activePlatforms:@[                            @(SSDKPlatformTypeSinaWeibo),                            @(SSDKPlatformSubTypeWechatSession),                            @(SSDKPlatformSubTypeWechatTimeline),                            @(SSDKPlatformTypeQQ)                            ]                 onImport:^(SSDKPlatformType platformType) {                                          switch (platformType)                     {                         case SSDKPlatformTypeWechat:                             [ShareSDKConnector connectWeChat:[WXApi class]];                             [ShareSDKConnector connectWeChat:[WXApi class] delegate:self];                             break;                         case SSDKPlatformTypeQQ:                             [ShareSDKConnector connectQQ:[QQApiInterface class]                                        tencentOAuthClass:[TencentOAuth class]];                             break;                         case SSDKPlatformTypeSinaWeibo:                             [ShareSDKConnector connectWeibo:[WeiboSDK class]];                             break;                         default:                             break;                     }                 }          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {                            switch (platformType)              {                  case SSDKPlatformTypeSinaWeibo:                      //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权                      [appInfo SSDKSetupSinaWeiboByAppKey:@"2122649601"                                                appSecret:@"4f2923d4f6657cb98a35b8e1ec8a3dr6"                                              redirectUri:@"http://open.weibo.com/apps/"                                                 authType:SSDKAuthTypeBoth];                      break;                  case SSDKPlatformTypeTencentWeibo:                      //设置腾讯微博应用信息,其中authType设置为只用Web形式授权                      [appInfo SSDKSetupTencentWeiboByAppKey:@"801207650"                                                   appSecret:@"ae36f4ee3946e1cbb18d6965b0b2ff5c"                                                 redirectUri:@"http://www.sharesdk.cn"];                      break;                  case SSDKPlatformTypeWechat:                      [appInfo SSDKSetupWeChatByAppId:@"wx8c206faf30cb4ba7"                                            appSecret:@"0ef244c823dc1302a91b50a99ee70e4f"];                      break;                  case SSDKPlatformTypeQQ:                      [appInfo SSDKSetupQQByAppId:@"1105459316"                                           appKey:@"ACed2xqrNZimx3gF"                                         authType:SSDKAuthTypeBoth];                      break;                  default:                      break;              }          }];}


点击分享按钮后就会执行以下程序,这里显示分享菜单内容以及处理分享结果。showShareActionSheet中Items列出的平台顺序指定了分享视图中APP的显示顺序。

- (void)shareShow:(NSString *)shareUrl{        /**     * 在简单分享中,只要设置共有分享参数即可分享到任意的社交平台     **/    __weak ViewController *theController = self;        //1、创建分享参数(必要)    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];            /* 打开客户端 */    [shareParams SSDKEnableUseClientShare];        NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];    [shareParams SSDKSetupShareParamsByText:@"分享内容"                                     images:imageArray                                        url:[NSURL URLWithString:@"http://www.star-v.com.cn"]                                      title:@"分享标题"                                       type:SSDKContentTypeAuto];        //1.2、自定义分享平台(非必要)    NSMutableArray *activePlatforms = [NSMutableArray arrayWithArray:[ShareSDK activePlatforms]];    //添加一个自定义的平台(非必要)    SSUIShareActionSheetCustomItem *item = [SSUIShareActionSheetCustomItem itemWithIcon:[UIImage imageNamed:@"Icon.png"]                                                                                  label:@"自定义"                                                                                onClick:^{                                                                                                                                                                        //自定义item被点击的处理逻辑                                                                                    NSLog(@"=== 自定义item被点击 ===");                                                                                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"自定义item被点击"                                                                                                                                        message:nil                                                                                                                                       delegate:nil                                                                                                                              cancelButtonTitle:@"确定"                                                                                                                              otherButtonTitles:nil];                                                                                    [alertView show];                                                                                }];    [activePlatforms addObject:item];        //设置分享菜单栏样式(非必要)    [SSUIShareActionSheetStyle setActionSheetBackgroundColor:[UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:0.5]];    [SSUIShareActionSheetStyle setActionSheetColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:0.8]];    [SSUIShareActionSheetStyle setCancelButtonBackgroundColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:0.8]];    [SSUIShareActionSheetStyle setCancelButtonLabelColor:[UIColor whiteColor]];    [SSUIShareActionSheetStyle setItemNameColor:[UIColor whiteColor]];    [SSUIShareActionSheetStyle setItemNameFont:[UIFont systemFontOfSize:10]];    [SSUIShareActionSheetStyle setCurrentPageIndicatorTintColor:[UIColor colorWithRed:156/255.0 green:156/255.0 blue:156/255.0 alpha:1.0]];    [SSUIShareActionSheetStyle setPageIndicatorTintColor:[UIColor colorWithRed:62/255.0 green:62/255.0 blue:62/255.0 alpha:1.0]];        //2、分享    SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:self.view                             items:@[@(SSDKPlatformTypeQQ),@(SSDKPlatformSubTypeWechatSession),@(SSDKPlatformSubTypeWechatTimeline),@(SSDKPlatformTypeSinaWeibo)]                       shareParams:shareParams               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {                                      switch (state) {                                                  case SSDKResponseStateBegin:                       {                           //[theController showLoadingView:YES];                           break;                       }                       case SSDKResponseStateSuccess:                       {                           //Facebook Messenger、WhatsApp等平台捕获不到分享成功或失败的状态,最合适的方式就是对这些平台区别对待                           if (platformType == SSDKPlatformTypeFacebookMessenger)                           {                               break;                           }                                                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"                                                                               message:nil                                                                              delegate:nil                                                                     cancelButtonTitle:@"确定"                                                                     otherButtonTitles:nil];                           [alertView show];                           break;                       }                       case SSDKResponseStateFail:                       {                           if (platformType == SSDKPlatformTypeSMS && [error code] == 201)                           {                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"                                                                               message:@"失败原因可能是:1、短信应用没有设置帐号;2、设备不支持短信应用;3、短信应用在iOS 7以上才能发送带附件的短信。"                                                                              delegate:nil                                                                     cancelButtonTitle:@"OK"                                                                     otherButtonTitles:nil, nil];                               [alert show];                               break;                           }                           else if(platformType == SSDKPlatformTypeMail && [error code] == 201)                           {                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"                                                                               message:@"失败原因可能是:1、邮件应用没有设置帐号;2、设备不支持邮件应用;"                                                                              delegate:nil                                                                     cancelButtonTitle:@"OK"                                                                     otherButtonTitles:nil, nil];                               [alert show];                               break;                           }                           else                           {                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"                                                                               message:[NSString stringWithFormat:@"%@",error]                                                                              delegate:nil                                                                     cancelButtonTitle:@"OK"                                                                     otherButtonTitles:nil, nil];                               [alert show];                               break;                           }                           break;                       }                       case SSDKResponseStateCancel:                       {                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"                                                                               message:nil                                                                              delegate:nil                                                                     cancelButtonTitle:@"确定"                                                                     otherButtonTitles:nil];                           [alertView show];                           break;                       }                       default:                           break;                   }                                  }];        [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];}



添加以上代码后并不能完成分享,还需要下面步骤。

添加白名单

从IOS9.0后,涉及到平台客户端的跳转,系统会自动到info.plist下检查是否设置Scheme。如果没有相应的配置,就无法跳转到相应的客户端。因此如果客户端集成有分享与授权,需要配置Scheme白名单。

  1. 在info.plist增加key:LSApplicationQueriesSchemes,类型为NSArray。
  2. 添加需要支持的白名单,类型为String。


实现SSO授权(SSO授权登录<无需用户输入密码登录>)

打开Info.plist,找到URL types配置项(如果没有则新增),展开URL types – URL Schemes,在URL Schemes下分别各新增一项应用的Scheme。其填写格式为:wb+appKey, 如:wb2279784657。如图所示:



运行过程中还可能报如下错误:App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.打开Info.plist,找到URL types配置项(如果没有则新增),做如下配置:


1 0
原创粉丝点击