本篇文章项目demo:点击打开链接https://github.com/zhonggaorong/weiboSDKDemo
微博环境的相关搭建,请参照我的这篇博客 : http://blog.csdn.net/zhonggaorong/article/details/51724810
1. 分享的前提也是您是微博的开发者了,有了对应的appid app,还有回调地址。
2. 已经配置好相应的开发环境,如: url scheme的配置, 白名单的配置。
微博分享坑的总结: (不能怪微博,怪自己太粗心)
1. 没有向 微博进行注册,导致,分享不成功
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[WeiboSDK registerApp:APP_KEY];
return YES;
}
WBWebpageObject *webObject = [WBWebpageObject object];
// 不能为空,否则会失败
webObject.webpageUrl = @"http://www.baidu.com";
webObject.objectID = @"dd";
//title 不能为空
webObject.title = @"分享";
webObject.description = @"详情内容---哈哈哈";
//缩略图
// webObject.thumbnailData = UIImageJPEGRepresentation(image2, 1.0);
message.mediaObject = webObject;
3.不管是登录还是分享,一定记得 把 bundle id 与 申请时候的 一致,否则调试不成功
下面是具体代码:
appDelegate.h
#import
#import "WeiboSDK.h"
@protocol WeiBoDelegate
//登录的代理
-(void)weiboLoginByResponse:(WBBaseResponse *)response;
//分享的代理
-(void)weiboShareSuccessCode:(NSInteger)shareResultCode;
@end
@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@property (weak , nonatomic) id weiboDelegate;
@end
appDelegate.m
#import "AppDelegate.h"
#import "WeiboSDK.h"
//申请下来的appkey
#define APP_KEY @"APP KEY"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[WeiboSDK registerApp:APP_KEY];
return YES;
}
// 9.0 后才生效
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options{
return [WeiboSDK handleOpenURL:url delegate:self];
}
#pragma mark 9.0之前
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [WeiboSDK handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{
return [WeiboSDK handleOpenURL:url delegate:self];
}
/**
收到一个来自微博客户端程序的请求
收到微博的请求后,第三方应用应该按照请求类型进行处理,处理完后必须通过 [WeiboSDK sendResponse:] 将结果回传给微博
@param request 具体的请求对象
*/
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request{ //向微博发送请求
NSLog(@" %@",request.class);
}
/**
微博分享 与 微博登录,成功与否都会走这个方法。 用户根据自己的业务进行处理。
收到一个来自微博客户端程序的响应
收到微博的响应后,第三方应用可以通过响应类型、响应的数据和 WBBaseResponse.userInfo 中的数据完成自己的功能
@param response 具体的响应对象
*/
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response{
if ([response isKindOfClass:WBAuthorizeResponse.class]) //微博登录的回调
{
if ([_weiboDelegate respondsToSelector:@selector(weiboLoginByResponse:)]) {
[_weiboDelegate weiboLoginByResponse:response];
}
}
if ([response isKindOfClass:WBSendMessageToWeiboResponse.class]) { //微博分享的回调
WBSendMessageToWeiboResponse *res = (WBSendMessageToWeiboResponse *)response;
if ([_weiboDelegate respondsToSelector:@selector(weiboShareSuccessCode:)]) {
[_weiboDelegate weiboShareSuccessCode:res.statusCode];
}
}
} @end
ViewController.h
#import
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
#import "WeiboSDK.h"
#import "AppDelegate.h"
#define APP_REDIRECT_URL @"在微博开发者后台,填写的回调地址"
#define APP_KEY @"APP KEY"
@interface ViewController ()
{
AppDelegate *delgate;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
delgate = (AppDelegate *)[UIApplication sharedApplication].delegate;
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)weiboLoginAction:(id)sender {
delgate.weiboDelegate = self;
WBAuthorizeRequest *request = [WBAuthorizeRequest request];
//回调地址与 新浪微博开放平台中 我的应用 --- 应用信息 -----高级应用 -----授权设置 ---应用回调中的url保持一致就好了
request.redirectURI = APP_REDIRECT_URL;
//SCOPE 授权说明参考 http://open.weibo.com/wiki/
request.scope = @"all";
request.userInfo = nil;
[WeiboSDK sendRequest:request];
}
-(void)weiboLoginByResponse:(WBBaseResponse *)response{
NSDictionary *dic = (NSDictionary *) response.requestUserInfo;
NSLog(@"userinfo %@",dic);
}
- (IBAction)weiboShareAction:(id)sender {
//微博分享、需要授权
WBAuthorizeRequest *authorize = [WBAuthorizeRequest request];
authorize.redirectURI = @"3125116264";
authorize.scope = @"all";
authorize.userInfo = nil;
WBMessageObject *message = [WBMessageObject message];
//分享的具体描述内容
message.text = @"微博分享测试";
/** 分享的内容分为两种 , 消息的图片内容, 还有一种是消息的多媒体内容
消息的图片内容
@see WBImageObject
消息的多媒体内容
@see WBBaseMediaObject
WBVideoObject 消息中包含的视频数据对象
WBMusicObject 消息中包含的音乐数据对象
WBWebpageObject 消息中包含的网页数据对象
*/
//第一种分享方式: 消息的图片内容
// UIImage* image2 = [UIImage imageNamed:@"首饰.jpg"];
// WBImageObject *imageObject = [WBImageObject object];
// imageObject.imageData = UIImageJPEGRepresentation(image2, 1.0);
// message.imageObject = imageObject;
//第二种分享方式: 多媒体内容分享
WBWebpageObject *webObject = [WBWebpageObject object];
// 不能为空,否则会失败
webObject.webpageUrl = @"http://www.baidu.com";
webObject.objectID = @"dd";
//title 不能为空
webObject.title = @"分享";
webObject.description = @"详情内容---哈哈哈";
//缩略图
// webObject.thumbnailData = UIImageJPEGRepresentation(image2, 1.0);
message.mediaObject = webObject;
/**
返回一个 WBSendMessageToWeiboRequest 对象
当用户安装了可以支持微博客户端內分享的微博客户端时,会自动唤起微博并分享
当用户没有安装微博客户端或微博客户端过低无法支持通过客户端內分享的时候会自动唤起SDK內微博发布器
@param message 需要发送给微博的消息对象
@param authRequest 授权相关信息,与access_token二者至少有一个不为空,当access_token为空并且需要弹出SDK內发布器时会通过此信息先进行授权后再分享
@param access_token 第三方应用之前申请的Token,当此值不为空并且无法通过客户端分享的时候,会使用此token进行分享。
@return 返回一个*自动释放的*WBSendMessageToWeiboRequest对象
*/
WBSendMessageToWeiboRequest *weiboRequest = [WBSendMessageToWeiboRequest requestWithMessage:message
authInfo:authorize
access_token:nil];
// WBSendMessageToWeiboRequest *weiboRequest = [WBSendMessageToWeiboRequest requestWithMessage:message];
weiboRequest.userInfo = nil;
delgate.weiboDelegate = self;
BOOL isSuccess = [WeiboSDK sendRequest:weiboRequest];
NSLog(@"分享是否成功 %d",isSuccess);
}
#pragma mark 微博分享 代理回调。
/* 回调时候 代码说明
WeiboSDKResponseStatusCodeSuccess = 0,//成功
WeiboSDKResponseStatusCodeUserCancel = -1,//用户取消发送
WeiboSDKResponseStatusCodeSentFail = -2,//发送失败
WeiboSDKResponseStatusCodeAuthDeny = -3,//授权失败
WeiboSDKResponseStatusCodeUserCancelInstall = -4,//用户取消安装微博客户端
WeiboSDKResponseStatusCodePayFail = -5,//支付失败
WeiboSDKResponseStatusCodeShareInSDKFailed = -8,//分享失败 详情见response UserInfo
WeiboSDKResponseStatusCodeUnsupport = -99,//不支持的请求
WeiboSDKResponseStatusCodeUnknown = -100,
*/
-(void)weiboShareSuccessCode:(NSInteger)shareResultCode{
NSLog(@"result code %ld",(long)shareResultCode);
if (shareResultCode == 0) {
UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"分享成功" message:@"恭喜,获取优惠券一张" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
[aler show];
}
}
@end