shareSDK 主要是分享+第三方登陆所需要的准备:
1.shareSDK 注册会员并创建应用+下载SDK
2.shareSDK--》管理应用--》社会化平台设置:配置托管个平台(微薄+微信+QQ等开放平台)的appkey
以上已经做好了准备,接下来是代码了
1.项目嵌入shareSDK 所需的 framework就不说了,官网介绍,
2.注册shareSDK 并且初始化 所需要分享的平台
#pragma mark - 分享初始化我上面是用到几个 没有全部初始化出来
-(void)ShareSDKInit
{
[ShareSDK registerApp:@"1e5eaf7da57b"];
//添加新浪微博应用 注册网址 http://open.weibo.com
[ShareSDK connectSinaWeiboWithAppKey:@"529394706"
appSecret:@"a899c06717bdbec8ab32264dd2dbfc45"
redirectUri:@"http://223.4.32.64:21910/manage/webapp/lp_main.aspx"];
[ShareSDK connectQQWithQZoneAppKey:@"100371282"
qqApiInterfaceCls:[QQApiInterface class]
tencentOAuthCls:[TencentOAuth class]];
//连接微信应用以使用相关功能,此应用需要引用WeChatConnection.framework和微信官方SDK
//http://open.weixin.qq.com上注册应用,并将相关信息填写以下字段
[ShareSDK connectWeChatWithAppId:@"wx5e5c77a45387a481" wechatCls:[WXApi class]];
[ShareSDK connectQZoneWithAppKey:@"100371282"
appSecret:@"aed9b0303e3ed1e27bae87c33761161d"
qqApiInterfaceCls:[QQApiInterface class]
tencentOAuthCls:[TencentOAuth class]];
}
弹出分享页面
#pragma mark - 弹出分享
-(void)ShowSharePlatform
{
NSString *imagePath = [[NSBundle mainBundle] pathForResource:IMAGE_NAME ofType:IMAGE_EXT];
//构造分享内容
idpublishCOntent= [ShareSDK content:CONTENT
defaultContent:@""
image:[ShareSDK imageWithPath:imagePath]
title:@"ShareSDK"
url:@"http://223.4.32.64:21910/manage/webapp/lp_main.aspx"
description:NSLocalizedString(@"TEXT_TEST_MSG", @"这是一条测试信息")
mediaType:SSPublishContentMediaTypeNews];
///////////////////////
//以下信息为特定平台需要定义分享内容,如果不需要可省略下面的添加方法
//定制人人网信息
[publishContent addRenRenUnitWithName:NSLocalizedString(@"TEXT_HELLO_RENREN", @"Hello 人人网")
description:INHERIT_VALUE
url:INHERIT_VALUE
message:INHERIT_VALUE
image:INHERIT_VALUE
caption:nil];
//定制QQ空间信息
[publishContent addQQSpaceUnitWithTitle:NSLocalizedString(@"TEXT_HELLO_QZONE", @"Hello QQ空间")
url:INHERIT_VALUE
site:nil
fromUrl:nil
comment:INHERIT_VALUE
summary:INHERIT_VALUE
image:INHERIT_VALUE
type:INHERIT_VALUE
playUrl:nil
nswb:nil];
//定制微信好友信息
[publishContent addWeixinSessionUnitWithType:INHERIT_VALUE
content:INHERIT_VALUE
title:NSLocalizedString(@"TEXT_HELLO_WECHAT_SESSION", @"Hello 微信好友!")
url:INHERIT_VALUE
thumbImage:[ShareSDK imageWithUrl:@"http://img1.bdstatic.com/img/image/67037d3d539b6003af38f5c4c4f372ac65c1038b63f.jpg"]
image:INHERIT_VALUE
musicFileUrl:nil
extInfo:nil
fileData:nil
emoticonData:nil];
//定制微信朋友圈信息
[publishContent addWeixinTimelineUnitWithType:[NSNumber numberWithInteger:SSPublishContentMediaTypeMusic]
content:INHERIT_VALUE
title:NSLocalizedString(@"TEXT_HELLO_WECHAT_TIMELINE", @"Hello 微信朋友圈!")
url:@"http://y.qq.com/i/song.html#p=7B22736F6E675F4E616D65223A22E4BDA0E4B88DE698AFE79C9FE6ADA3E79A84E5BFABE4B990222C22736F6E675F5761704C69766555524C223A22687474703A2F2F74736D7573696332342E74632E71712E636F6D2F586B303051563558484A645574315070536F4B7458796931667443755A68646C2F316F5A4465637734356375386355672B474B304964794E6A3770633447524A574C48795333383D2F3634363232332E6D34613F7569643D32333230303738313038266469723D423226663D312663743D3026636869643D222C22736F6E675F5769666955524C223A22687474703A2F2F73747265616D31382E71716D757369632E71712E636F6D2F33303634363232332E6D7033222C226E657454797065223A2277696669222C22736F6E675F416C62756D223A22E5889BE980A0EFBC9AE5B08FE5B7A8E89B8B444E414C495645EFBC81E6BC94E594B1E4BC9AE5889BE7BAAAE5BD95E99FB3222C22736F6E675F4944223A3634363232332C22736F6E675F54797065223A312C22736F6E675F53696E676572223A22E4BA94E69C88E5A4A9222C22736F6E675F576170446F776E4C6F616455524C223A22687474703A2F2F74736D757369633132382E74632E71712E636F6D2F586C464E4D31354C5569396961495674593739786D436534456B5275696879366A702F674B65356E4D6E684178494C73484D6C6A307849634A454B394568572F4E3978464B316368316F37636848323568413D3D2F33303634363232332E6D70333F7569643D32333230303738313038266469723D423226663D302663743D3026636869643D2673747265616D5F706F733D38227D"
thumbImage:[ShareSDK imageWithUrl:@"http://img1.bdstatic.com/img/image/67037d3d539b6003af38f5c4c4f372ac65c1038b63f.jpg"]
image:INHERIT_VALUE
musicFileUrl:@"http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3"
extInfo:nil
fileData:nil
emoticonData:nil];
//定制微信收藏信息
[publishContent addWeixinFavUnitWithType:INHERIT_VALUE
content:INHERIT_VALUE
title:NSLocalizedString(@"TEXT_HELLO_WECHAT_FAV", @"Hello 微信收藏!")
url:INHERIT_VALUE
thumbImage:[ShareSDK imageWithUrl:@"http://img1.bdstatic.com/img/image/67037d3d539b6003af38f5c4c4f372ac65c1038b63f.jpg"]
image:INHERIT_VALUE
musicFileUrl:nil
extInfo:nil
fileData:nil
emoticonData:nil];
//定制QQ分享信息
[publishContent addQQUnitWithType:INHERIT_VALUE
content:INHERIT_VALUE
title:@"Hello QQ!"
url:INHERIT_VALUE
image:INHERIT_VALUE];
//定制邮件信息
[publishContent addMailUnitWithSubject:@"Hello Mail"
content:INHERIT_VALUE
isHTML:[NSNumber numberWithBool:YES]
attachments:INHERIT_VALUE
to:nil
cc:nil
bcc:nil];
//定制短信信息
[publishContent addSMSUnitWithContent:@"Hello SMS"];
//定制有道云笔记信息
[publishContent addYouDaoNoteUnitWithContent:INHERIT_VALUE
title:NSLocalizedString(@"TEXT_HELLO_YOUDAO_NOTE", @"Hello 有道云笔记")
author:@"ShareSDK"
source:nil
attachments:INHERIT_VALUE];
//定制Instapaper信息
[publishContent addInstapaperContentWithUrl:INHERIT_VALUE
title:@"Hello Instapaper"
description:INHERIT_VALUE];
//定制搜狐随身看信息
[publishContent addSohuKanUnitWithUrl:INHERIT_VALUE];
//结束定制信息
////////////////////////
//创建弹出菜单容器
idcOntainer= [ShareSDK container];
[container setIPadContainerWithView:nil arrowDirect:UIPopoverArrowDirectionUp];
idauthOptiOns= [ShareSDK authOptionsWithAutoAuth:YES
allowCallback:NO
authViewStyle:SSAuthViewStyleFullScreenPopup
viewDelegate:nil
authManagerViewDelegate:nil];
//在授权页面中添加关注官方微博
[authOptions setFollowAccounts:[NSDictionary dictionaryWithObjectsAndKeys:
[ShareSDK userFieldWithType:SSUserFieldTypeName value:@"ShareSDK"],
SHARE_TYPE_NUMBER(ShareTypeSinaWeibo),
[ShareSDK userFieldWithType:SSUserFieldTypeName value:@"ShareSDK"],
SHARE_TYPE_NUMBER(ShareTypeTencentWeibo),
nil]];
idshareOptiOns= [ShareSDK simpleShareOptionsWithTitle:NSLocalizedString(@"TEXT_SHARE_TITLE", @"内容分享")
shareViewDelegate:nil];
//弹出分享菜单
[ShareSDK showShareActionSheet:container
shareList:nil
content:publishContent
statusBarTips:YES
authOptions:authOptions
shareOptions:shareOptions
result:^(ShareType type, SSResponseState state, idstatusInfo, id error, BOOL end) {
if (state == SSPublishContentStateSuccess)
{
NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"分享成功"));
}
else if (state == SSPublishContentStateFail)
{
NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
}
}];
}
a。配置plish文件(配置各平台的Appkey)
b。appdelegate 里面要弄好分享回调
#pragma mark - 分享回调
- (BOOL)application:(UIApplication *)application
handleOpenURL:(NSURL *)url
{
return [ShareSDK handleOpenURL:url
wxDelegate:self];
}
1,还是注册shareSDK +初始化平台 代码同上
2,授权用户信息的时候调用该事件 type :微薄 还是 QQ 类型
#pragma mark - 初始化 授权获取第三方用户信息3.授权后会回调授权得到用户信息:
-(void)initWithType:(ShareType)type
{
_infoDict = [[NSMutableDictionary alloc] init];
_type = type;
_flag = 0;
if (!_initialized)
{
_initialized = YES;
idauthOptiOns= [ShareSDK authOptionsWithAutoAuth:YES
allowCallback:YES
authViewStyle:SSAuthViewStyleFullScreenPopup
viewDelegate:nil
authManagerViewDelegate:nil];
//在授权页面中添加关注官方微博
[authOptions setFollowAccounts:[NSDictionary dictionaryWithObjectsAndKeys:
[ShareSDK userFieldWithType:SSUserFieldTypeName value:@"ShareSDK"],
SHARE_TYPE_NUMBER(ShareTypeSinaWeibo),
[ShareSDK userFieldWithType:SSUserFieldTypeName value:@"ShareSDK"],
SHARE_TYPE_NUMBER(ShareTypeTencentWeibo),
nil]];
if (_flag == 0)
{
[ShareSDK getUserInfoWithType:_type
authOptions:authOptions
result:^(BOOL result, iduserInfo, id error) {
if (result)
{
[_infoDict removeAllObjects];
if ([userInfo profileImage])
{
[NSThread detachNewThreadSelector:@selector(loadImage:)
toTarget:self
withObject:[userInfo profileImage]];
}
switch (_type)
{
case ShareTypeSinaWeibo:
//新浪微博
[self fillSinaWeiboUser:userInfo];
break;
case ShareTypeQQSpace:
//QQ空间
[self fillQQSpaceUser:userInfo];
break;
default:
break;
}
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TEXT_TIPS", @"提示")
message:error.errorDescription
delegate:nil
cancelButtonTitle:NSLocalizedString(@"TEXT_KNOW", @"知道了")
otherButtonTitles: nil];
[alertView show];
}
}];
}
else
{
[ShareSDK getUserInfoWithType:_type
field:_name
fieldType:_paramType
authOptions:authOptions
result:^(BOOL result, iduserInfo, id error) {
if (result)
{
[_infoDict removeAllObjects];
if ([userInfo profileImage])
{
[NSThread detachNewThreadSelector:@selector(loadImage:)
toTarget:self
withObject:[userInfo profileImage]];
}
switch (_type)
{
case ShareTypeSinaWeibo:
//新浪微博
[self fillSinaWeiboUser:userInfo];
break;
case ShareTypeQQSpace:
//QQ空间
[self fillQQSpaceUser:userInfo];
break;
default:
break;
}
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TEXT_TIPS", @"提示")
message:error.errorDescription
delegate:nil
cancelButtonTitle:NSLocalizedString(@"TEXT_KNOW", @"知道了")
otherButtonTitles: nil];
[alertView show];
}
}];
}
}
}
#pragma mark - 获取微博个人信息
- (void)fillSinaWeiboUser:(id)userInfo
{
NSArray *keys = [[userInfo sourceData] allKeys];
for (int i = 0; i <[keys count]; i++)
{
NSString *keyName = [keys objectAtIndex:i];
id value = [[userInfo sourceData] objectForKey:keyName];
if (![value isKindOfClass:[NSString class]])
{
if ([value respondsToSelector:@selector(stringValue)])
{
value = [value stringValue];
}
else
{
value = @"";
}
}
if ([keyName isEqualToString:@"id"])
{
[_infoDict setObject:value forKey:@"UID"];
}
else if([keyName isEqualToString:@"idstr"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_UID_STR", @"字符串型UID")];
}
else if([keyName isEqualToString:@"screen_name"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_NICKNAME", @"用户昵称")];
}
else if([keyName isEqualToString:@"name"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_SCREEN_NAME", @"显示名称")];
}
else if([keyName isEqualToString:@"province"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_PROVINCE_ID", @"省级ID")];
}
else if([keyName isEqualToString:@"city"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_CITY_ID", @"城市ID")];
}
else if([keyName isEqualToString:@"location"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_LOCATION", @"所在地")];
}
else if([keyName isEqualToString:@"description"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_DESCRIPTION", @"个人描述")];
}
else if([keyName isEqualToString:@"url"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_BLOG_URL", @"博客地址")];
}
else if([keyName isEqualToString:@"profile_image_url"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_AVATAR_URL", @"头像地址")];
}
else if([keyName isEqualToString:@"profile_url"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_WEIBO_URL", @"微博统一URL地址")];
}
else if([keyName isEqualToString:@"domain"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_CUSTOM_DOMAIN", @"个性化域名")];
}
else if([keyName isEqualToString:@"weihao"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_WEIHAO", @"微号")];
}
else if([keyName isEqualToString:@"gender"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_GENDER", @"性别")];
}
else if([keyName isEqualToString:@"followers_count"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_FANS_COUNT", @"粉丝数")];
}
else if([keyName isEqualToString:@"friends_count"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_FOLLOW_COUNT", @"关注数")];
}
else if([keyName isEqualToString:@"statuses_count"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_WEIBO_COUNT", @"微博数")];
}
else if([keyName isEqualToString:@"favourites_count"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_FAV_COUNT", @"收藏数")];
}
else if([keyName isEqualToString:@"created_at"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_REG_TIME", @"注册时间")];
}
else if([keyName isEqualToString:@"following"])
{
[_infoDict setObject:value forKey:@"following"];
}
else if([keyName isEqualToString:@"allow_all_act_msg"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_ALLOW_SEND_LETTER", @"是否允许给我发私信")];
}
else if([keyName isEqualToString:@"geo_enabled"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_ALLOW_GEO", @"是否允许标识地理位置")];
}
else if([keyName isEqualToString:@"verified"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_IS_WEIBO_VERIFIED_USER", @"是否是微博认证用户")];
}
else if([keyName isEqualToString:@"verified_type"])
{
[_infoDict setObject:value forKey:@"verified_type"];
}
else if([keyName isEqualToString:@"remark"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_REMARK", @"备注信息")];
}
else if([keyName isEqualToString:@"allow_all_comment"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_ALLOW_COMMENT_STATUS", @"是否允许对我的微博进行评论")];
}
else if([keyName isEqualToString:@"avatar_large"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_LARGE_AVATAR_URL", @"大头像地址")];
}
else if([keyName isEqualToString:@"verified_reason"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_VERIFIED_REASON", @"认证原因")];
}
else if([keyName isEqualToString:@"follow_me"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_FOLLOW_ME", @"是否关注我")];
}
else if([keyName isEqualToString:@"online_status"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_ONLINE_STATUS", @"在线状态")];
}
else if([keyName isEqualToString:@"bi_followers_count"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_BI_FOLLOWER_COUNT", @"互粉数")];
}
else if([keyName isEqualToString:@"lang"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_LANG", @"语言版本")];
}
}
}
#pragma mark - 获取QQ授权信息
- (void)fillQQSpaceUser:(id)userInfo
{
NSArray *keys = [[userInfo sourceData] allKeys];
for (int i = 0; i <[keys count]; i++)
{
NSString *keyName = [keys objectAtIndex:i];
id value = [[userInfo sourceData] objectForKey:keyName];
if (![value isKindOfClass:[NSString class]])
{
if ([value respondsToSelector:@selector(stringValue)])
{
value = [value stringValue];
}
else
{
value = @"";
}
}
if ([keyName isEqualToString:@"figureurl"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_FIGURE_URL", @"30×30头像URL")];
}
else if ([keyName isEqualToString:@"figureurl_1"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_FIGURE_URL_1", @"50×50头像URL")];
}
else if ([keyName isEqualToString:@"figureurl_2"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_FIGURE_URL_2", @"100×100头像URL")];
}
else if ([keyName isEqualToString:@"gender"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_GENDER", @"性别")];
}
else if ([keyName isEqualToString:@"is_yellow_year_vip"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_IS_YELLOW_YEAR_VIP", @"是否为年费黄钻用户")];
}
else if ([keyName isEqualToString:@"level"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_YELLOW_LEVEL", @"黄钻等级")];
}
else if ([keyName isEqualToString:@"nickname"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_NICKNAME", @"昵称")];
}
else if ([keyName isEqualToString:@"vip"])
{
[_infoDict setObject:value forKey:NSLocalizedString(@"TEXT_YELLOW_VIP", @"是否为黄钻用户")];
}
}
}
#pragma mark - 用户信息
- (void)loadImage:(NSString *)url
{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
}
#pragma mark - 用户信息
- (void)showUserIcon:(UIImage *)icon
{
if (icon)
{
}
}
a。配饰plish的appkey(同上)
b。授权回调(appdelegate)
#pragma mark - 授权回调
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [ShareSDK handleOpenURL:url
sourceApplication:sourceApplication
annotation:annotation
wxDelegate:self];
}