iOS第三方分享-ShareSDK
网址链接:http://mob.com/Download/detail?type=1&plat=2
由于新版的简单分享很多功能都有,而且打包后体积比全版本的少了几M所以在这里用这个
1.在官网下载SDK并导入相应的framework(比起之前的少了很多库)
2.初始化ShareSDK(因为,一般分享的话就这几个平台,全部集成下)
AppDelegate中导入头文件
//ShareSDK必要头文件 #import <ShareSDK/ShareSDK.h> #import <ShareSDKConnector/ShareSDKConnector.h> //腾讯开放平台(对应QQ和QQ空间)SDK头文件 #import <TencentOpenAPI/TencentOAuth.h> #import <TencentOpenAPI/QQApiInterface.h> //微信SDK头文件 #import "WXApi.h" //新浪微博SDK头文件 #import "WeiboSDK.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);
[ShareSDK registerApp:@"iosv1101" activePlatforms:@[ @(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeTencentWeibo), @(SSDKPlatformTypeMail), @(SSDKPlatformTypeSMS), @(SSDKPlatformTypeCopy), @(SSDKPlatformTypeFacebook), @(SSDKPlatformTypeTwitter), @(SSDKPlatformTypeWechat), @(SSDKPlatformTypeQQ), @(SSDKPlatformTypeDouBan)] onImport:^(SSDKPlatformType platformType) { switch (platformType) { case SSDKPlatformTypeWechat: [ShareSDKConnector connectWeChat:[WXApi class]]; break; case SSDKPlatformTypeQQ: [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]]; break; default: break; } } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) { switch (platformType) { case SSDKPlatformTypeSinaWeibo: //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权 [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243" appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3" redirectUri:@"http://www.sharesdk.cn" authType:SSDKAuthTypeBoth]; break; case SSDKPlatformTypeTencentWeibo: //设置腾讯微博应用信息,其中authType设置为只用Web形式授权 [appInfo SSDKSetupTencentWeiboByAppKey:@"801307650" appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c" redirectUri:@"http://www.sharesdk.cn"]; break; case SSDKPlatformTypeFacebook: //设置Facebook应用信息,其中authType设置为只用SSO形式授权 [appInfo SSDKSetupFacebookByAppKey:@"107704292745179" appSecret:@"38053202e1a5fe26c80c753071f0b573" authType:SSDKAuthTypeBoth]; break; case SSDKPlatformTypeTwitter: [appInfo SSDKSetupTwitterByConsumerKey:@"LRBM0H75rWrU9gNHvlEAA2aOy" consumerSecret:@"gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G" redirectUri:@"http://mob.com"]; break; case SSDKPlatformTypeWechat: [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885" appSecret:@"64020361b8ec4c99936c0e3999a9f249"]; break; case SSDKPlatformTypeQQ: [appInfo SSDKSetupQQByAppId:@"100371282" appKey:@"aed9b0303e3ed1e27bae87c33761161d" authType:SSDKAuthTypeBoth]; break; case SSDKPlatformTypeDouBan: [appInfo SSDKSetupDouBanByApiKey:@"02e2cbe5ca06de5908a863b15e149b0b" secret:@"9f1e7b4f71304f2f" redirectUri:@"http://www.sharesdk.cn"]; break; default: break; } }];
以上是ShareSDK测试用的key,自己做程序的时候需要在ShareSDK申请,各个平台的key也要在各个平台申请
3.ShareSDK的使用
导入头文件
#import <ShareSDK/ShareSDK.h> #import <ShareSDKExtension/SSEShareHelper.h> #import <ShareSDKUI/ShareSDK+SSUI.h> #import <ShareSDKUI/SSUIShareActionSheetStyle.h>
#pragma mark - 菜单分享 - (void)menuShareTest { NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; [shareParams SSDKSetupShareParamsByText:@"分享内容" images:nil url:[NSURL URLWithString:@"http://mob.com"] title:@"分享标题" type:SSDKContentTypeImage]; [ShareSDK showShareActionSheet:nil items:@[ @(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeTencentWeibo), @(SSDKPlatformSubTypeWechatSession), @(SSDKPlatformSubTypeWechatTimeline), @(SSDKPlatformSubTypeQQFriend), @(SSDKPlatformSubTypeQZone), @(SSDKPlatformTypeWechat)] shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { NSLog(@"-0-"); }]; //删除和添加 // [sheet.directSharePlatforms removeObject:@(SSDKPlatformTypeWechat)]; // [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)]; } #pragma mark - 单个分享 - (void)onlyShareTest { NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; [shareParams SSDKSetupShareParamsByText:@"分享内容" images:nil url:[NSURL URLWithString:@"http://mob.com"] title:@"分享标题" type:SSDKContentTypeImage]; [ShareSDK share:SSDKPlatformTypeSinaWeibo parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { switch (state) { case SSDKResponseStateSuccess: { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alertView show]; break; } case SSDKResponseStateFail: { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"%@", error] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alertView show]; break; } case SSDKResponseStateCancel: { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alertView show]; break; } default: break; } }]; } #pragma mark - 一键分享 - (void)onceShareTest { //构造分享参数 NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; [shareParams SSDKSetupShareParamsByText:@"我在使用ShareSDK的一键分享。" images:nil url:nil title:nil type:SSDKContentTypeImage]; [SSEShareHelper oneKeyShare:@[@(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeTencentWeibo)] parameters:nil onStateChanged:^(SSDKPlatformType platformType, SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { NSString *platformName = nil; switch (platformType) { case SSDKPlatformTypeSinaWeibo: platformName = @"新浪微博"; break; case SSDKPlatformTypeTencentWeibo: platformName = @"腾讯微博"; break; default: break; } }]; } #pragma mark - 第三方登录(信息获取) - (void)threeShareTest { [ShareSDK getUserInfo:SSDKPlatformTypeSinaWeibo onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) { NSLog(@"平台类型根据枚举查看:%ld",(unsigned long)user.platformType); NSLog(@"授权就不为空:%@",user.credential); NSLog(@"uid:%@",user.uid); NSLog(@"昵称:%@",user.nickname); NSLog(@"图片地址:%@",user.icon); NSLog(@"性别:%ld",(unsigned long)user.gender); }]; } #pragma mark - 其他 - (void)other { //判断是否授权 [ShareSDK hasAuthorized:SSDKPlatformTypeSinaWeibo]; }