您的位置:首页 > 其它

ShareSDK

2015-11-13 22:05 330 查看
1. 下载SDK--导入到项目中--在选择分享平台的时候,有些不用的就不选,尽量减少文件--http://www.mob.com/#/index

2. 添加相关库等,里面有文档,根据文档走。之后编译,一般有这个错误:



导入MessageUI框架即可,因为有些东西是基于MessageUI的。

在Build Settings -->Other linker flags中加入-ObjC标示

3. 下载的SDK中有个Demo,打开看下应该就明白了。

Demo里有个getUserInfo方法,是获取第三方登陆信息的,可以直接拿去用,下面以新浪微博为例,要包含头文件

#import <ShareSDK/ShareSDK.h>

首先在AppDelegate中的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中 注册SDK

//1.初始化ShareSDK应用,字符串"5559f92aa230"换成http://www.mob.com/后台申请的ShareSDK应用的Appkey
[ShareSDK registerApp:@"5559f92aa230"];

/**
连接新浪微博开放平台应用以使用相关功能,此应用需要引用SinaWeiboConnection.framework http://open.weibo.com上注册新浪微博开放平台应用,并将相关信息填写到以下字段 **/
[ShareSDK connectSinaWeiboWithAppKey:@"568898243"
appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
redirectUri:@"http://www.sharesdk.cn"
weiboSDKCls:[WeiboSDK class]];

//添加QQ应用  注册网址   http://mobile.qq.com/api/ [ShareSDK connectQQWithQZoneAppKey:@"100371282"
qqApiInterfaceCls:[QQApiInterface class]
tencentOAuthCls:[TencentOAuth class]];

//微信登陆的时候需要初始化
[ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885"
appSecret:@"64020361b8ec4c99936c0e3999a9f249"
wechatCls:[WXApi class]];


在info中添加微博APPKEY



然后再需要分享的地方获取第三方信息

//uid:3250903614 --nickname:编程小虾米---tooken:2.00QX9AYD0H7CVce083bbb34fhdGyDD
- (IBAction)weiboLogin:(UIButton *)sender {
[ShareSDK getUserInfoWithType:ShareTypeSinaWeibo
authOptions:nil
result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {

if (result)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:[NSString stringWithFormat:@"uid:%@ --nickname:%@---tooken:%@",[userInfo uid],[userInfo nickname],[[userInfo credential] token]]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];

[_userDict setValue:[userInfo uid] forKey:@"wbUid"];
[_userDict setValue:[userInfo nickname] forKey:@"wbName"];
[_userDict setValue:[[userInfo credential] token] forKey:@"wbToken"];

[self saveUserMsg];  //我自己存储信息的  请无视

NSLog(@"uid:%@ --nickname:%@---tooken:%@",[userInfo uid],[userInfo nickname],[[userInfo credential] token]);
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed"
message:[NSString stringWithFormat:@"error code: %zi,error description: %@",[error errorCode],[error errorDescription]]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
}];
}


最后就可以实现分享功能了

- (IBAction)share:(UIButton *)sender {
//1、构造分享内容
//1.1、要分享的图片(以下分别是网络图片和本地图片的生成方式的示例)
id<ISSCAttachment> remoteAttachment = [ShareSDKCoreService attachmentWithUrl:@"http://f.hiphotos.bdimg.com/album/w%3D2048/sign=df8f1fe50dd79123e0e09374990c5882/cf1b9d16fdfaaf51e6d1ce528d5494eef01f7a28.jpg"];
//        id<ISSCAttachment> localAttachment = [ShareSDKCoreService attachmentWithPath:[[NSBundle mainBundle] pathForResource:@"shareImg" ofType:@"png"]];

//1.2、以下参数分别对应:内容、默认内容、图片、标题、链接、描述、分享类型
id<ISSContent> publishContent = [ShareSDK content:@"给个面子看看吧"
defaultContent:nil
image:remoteAttachment
title:[self.mesageArr[sender.tag] title]
url:[self.mesageArr[sender.tag] imagesArr][0]
description:[self.mesageArr[sender.tag] dscription]
mediaType:SSPublishContentMediaTypeNews];

//1+、创建弹出菜单容器(iPad应用必要,iPhone应用非必要)
id<ISSContainer> container = [ShareSDK container];
[container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];

//2、展现分享菜单
[ShareSDK showShareActionSheet:container
shareList:nil
content:publishContent
statusBarTips:NO
authOptions:nil
shareOptions:nil
result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {

NSLog(@"=== response state :%zi ",state);

//可以根据回调提示用户。
if (state == SSResponseStateSuccess)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:nil
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
else if (state == SSResponseStateFail)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed"
message:[NSString stringWithFormat:@"Error Description:%@",[error errorDescription]]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: