首先下载新浪微博iOS SDK
接入说明以及SDK下载地址为:SDK下载地址
下载的目录下有一个名为“微博iOS平台SDK文档V3.1.1.pdf”文件,里面详细介绍了接入过程。
下面介绍一下自己的接入过程。
1.在appDelegate.m中添加微博代理
#pragma mark ------------- 微博登录 回调 -----------------
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
}
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
if ([responseisKindOfClass:WBAuthorizeResponse.class])
{
NSString * userid = [(WBAuthorizeResponse *)responseuserID];
NSString * wbtoken = [(WBAuthorizeResponse *)responseaccessToken];
NSString * oathString = [NSStringstringWithFormat:@"https://api.weibo.com/2/users/show.json?uid=%@&access_token=%@",userid,wbtoken];//
NSMutableString * msg = [NSMutableStringstring];
if (NO_VALUE(userid)) {
[msg setString:ALERT_WEIBO_LOGIN_ERROR];
}
NSDictionary* para = [NSDictionarydictionaryWithObjectsAndKeys:oathString,@"para",msg,@"msg",nil];
[[NSNotificationCenterdefaultCenter] postNotificationName:NOTIFICATION_WEIBO_LOGINobject:niluserInfo:para];
#if DEBUG
NSString *message = [NSStringstringWithFormat:@"%@: %d\nresponse.userId: %@\nresponse.accessToken: %@\n%@: %@\n%@: %@",NSLocalizedString(@"响应状态",nil), (int)response.statusCode,[(WBAuthorizeResponse *)responseuserID], [(WBAuthorizeResponse *)responseaccessToken], NSLocalizedString(@"响应UserInfo数据",nil), response.userInfo,NSLocalizedString(@"原请求UserInfo数据",nil), response.requestUserInfo];
MyNSLog(@"微博:%@",message);
#endif
}
}
//
// WeiboLoginClass.h
//
#import <Foundation/Foundation.h>
@class WeiboLoginClass;
@protocol WeiboLoginDelegate <NSObject>
@required
-(void) WeiboLogin:(WeiboLoginClass*) WeiboLoginClass Result:(NSDictionary*) resultDic;
@end
@interface WeiboLoginClass : NSObject
@property (assign)id <WeiboLoginDelegate> delegate;
@property (nonatomic,strong)UIViewController * currentVC;
- (instancetype)initWithViewController:(UIViewController*) vc;
- (void) startAuthorize;
@end
//
// WeiboLoginClass.m
//
#import "WeiboLoginClass.h"
@implementation WeiboLoginClass
- (instancetype)initWithViewController:(UIViewController*) vc
{
self = [superinit];
if (self) {
// Custom initialization
self.currentVC = vc;
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(getWeiboLoginNoti:)name:NOTIFICATION_WEIBO_LOGINobject:nil];
}
return self;
}
-(void) dealloc
{
[[NSNotificationCenterdefaultCenter] removeObserver:self];
}
-(void) startAuthorize
{
WBAuthorizeRequest *request = [WBAuthorizeRequestrequest];
request.redirectURI =WEIBO_REDIRECTURI;
request.scope = @"all";
request.userInfo =@{@"SSO_From":@"SendMessageToWeiboViewController",
@"Other_Info_1": [NSNumbernumberWithInt:123],
@"Other_Info_2": @[@"obj1", @"obj2"],
@"Other_Info_3":@{@"key1": @"obj1", @"key2": @"obj2"}};
[WeiboSDK sendRequest:request];
}
#pragma mark ---------- 微博登录 得到access_token后请求用户信息 --------------
-(void) getWeiboLoginNoti:(NSNotification*) noti
{
[self.currentVChideHud];
[[NSNotificationCenterdefaultCenter] removeObserver:self];
NSString* oath_url = [noti.userInfoobjectForKey:@"para"];
NSString* msg = [noti.userInfoobjectForKey:@"msg"];
if (NO_VALUE(msg)) {
dispatch_async(dispatch_get_main_queue(), ^{
#pragma warning -------------
NSError *err = nil;
NSMutableDictionary* dicData = [NSMutableDictionarydictionary];
err = [requestSyncServer requestSyncServerWithUrl:oath_url withReceiveDic:dicData];
if (err != nil) {
#if DEBUG
NSString* errString = [[NSStringalloc] initWithFormat:@"解析数据失败"];
MyNSLog(@"%@",errString);
#endif
[self.currentVCshowHint:ALERT_WEIBO_LOGIN_ERROR];
}else{
NSString* idstr = [dicData objectForKey:@"idstr"];
NSString* nickname = [dicData objectForKey:@"screen_name"];
NSString* avatar = [dicData objectForKey:@"avatar_large"];
if (!NO_VALUE(idstr) && !NO_VALUE(nickname) && !NO_VALUE(avatar)) {
// NSNumber* typeWeibo = [NSNumber numberWithInteger:3];
// [self.currentVC login2:[idstr lowercaseString] andNickname:nickname andAvatar:avatar andType:typeWeibo];
NSDictionary * resultDic = [NSDictionarydictionaryWithObjectsAndKeys:idstr,@"openid",avatar,@"avatar",nickname,@"nickname",nil];
[self.delegateWeiboLogin:selfResult:resultDic];
}
}
});
}else{
[self.currentVCshowHint:msg];
}
}
@end
#pragma mark ----------- 点击微博登录 --------
- (IBAction)touchWeiboLoginButton:(id)sender {
MyNSLog(@"点击微博登录");
self.weibologinclass = [[WeiboLoginClassalloc] initWithViewController:self];
self.weibologinclass.delegate =self;
[self.weibologinclassstartAuthorize];
}
#pragma mark
#pragma mark - Weibologindelegate
-(void) WeiboLogin:(WeiboLoginClass *)WeiboLoginClass Result:(NSDictionary *)resultDic
{
[self.openIDsetString:[resultDic objectForKey:@"openid"]];
[self.figureurl_qq_2setString:[resultDic objectForKey:@"avatar"]];
[self.nicknamesetString:[resultDic objectForKey:@"nickname"]];
[selflogin:self.openIDandNickname:self.nicknameandAvatar:self.figureurl_qq_2];
}