iOS 第三方登录之 新浪微博登录

首先下载新浪微博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

    }  

}

2.添加WeiboLoginClass.h文件,内容为:

//

//  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文件,内容为:

//

//  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


3. 在调起授权的viewcontroller中

#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];

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值