iOS 集成第三方微博登录功能

重点内容

详细集成微博登录功能的步骤如下:
1.先到官网下载微博SDK

2.将其导入你要集成的工程中

3.导入相关的系统依赖库即可
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
        [WeiboSDK enableDebugMode:YES];
        [WeiboSDK registerApp:kAppKey];
        
        return YES;  
}
    
#pragma mark - WeiboSDKDelegate
 - (void)didReceiveWeiboRequest:(WBBaseRequest *)request
 {
    
 }
 - (void)didReceiveWeiboResponse:(WBBaseResponse *)response
 {
        if ([response isKindOfClass:WBAuthorizeResponse.class])
        {
            NSString *title = NSLocalizedString(@"认证结果", nil);
            NSString *message = [NSString stringWithFormat:@"%@: %d\nresponse.userId: %@\nresponse.accessToken: %@\n%@: %@\n%@: %@",
    NSLocalizedString(@"响应状态", nil),
    (int)response.statusCode,[(WBAuthorizeResponse *)response userID],
    [(WBAuthorizeResponse *)response accessToken], 
    NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo,
    NSLocalizedString(@"原请求UserInfo数据", nil), response.requestUserInfo];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            
            self.wbtoken = [(WBAuthorizeResponse *)response accessToken];
            self.wbCurrentUserID = [(WBAuthorizeResponse *)response userID];
            self.wbRefreshToken = [(WBAuthorizeResponse *)response refreshToken];
            [alert show];
 } 
- (void)getUserInfo
{
        NSString *accessTokenRequestURLStr = [NSString stringWithFormat:@"https://api.weibo.com/2/users/show.json"];
        //    WBHttpRequest * wbRequest = [WBHttpRequest requestWithURL:@"https://api.weibo.com/2/users/show.json"
    httpMethod:@"GET" params:params delegate:self
    withTag:@"getUserInfo"];
        NSURL *accessTokenRequestURL = [NSURL URLWithString:accessTokenRequestURLStr];
    
        // 2,创建post请求
        NSMutableURLRequest *mutRequest = [[NSMutableURLRequest alloc]initWithURL:accessTokenRequestURL
    cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
        //设置请求方式为POST,默认为GET
        [mutRequest setHTTPMethod:@"GET"];
        
        // 3,连接服务器,并接收返回的数据
        NSData *receivedData = [NSURLConnection sendSynchronousRequest:mutRequest returningResponse:nil error:nil];
        // 将服务器返回的数据转成字串(实质是JSON数据)
        NSString *responseStr = [[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];
        NSLog(@"Response json is :%@",responseStr);
        
        NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:receivedData
    options:NSJSONReadingMutableContainers error:nil];
        
        // 通过键,取到access_token
        NSString *access_token = [dictionary objectForKey:@"access_token"];
        NSLog(@"access token is:%@",access_token);
        
        
        // 通过键,取到用户的uid
        NSString *uid = [dictionary objectForKey:@"openId"];
        NSString *nickName = [dictionary objectForKey:@"nickName"];
        NSLog(@"uid is:%@ nickName: %@",uid,nickName);
 }
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
 {
        return [WeiboSDK handleOpenURL:url delegate:self]; 
 }
- (BOOL)application:(UIApplication *)application handleOpenURL:(nonnull NSURL *)url
{
        return [WeiboSDK handleOpenURL:url delegate:self]; 
}
    
@implementation ViewController

 - (void)viewDidLoad 
{
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        UIButton *ssoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [ssoButton setTitle:NSLocalizedString(@"请求微博认证(SSO授权)", nil) forState:UIControlStateNormal];
        [ssoButton addTarget:self action:@selector(ssoButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        ssoButton.frame = CGRectMake(20, 90, 280, 40);
        ssoButton.backgroundColor = [UIColor grayColor];
        [self.view addSubview:ssoButton]; 
}     
- (void)ssoButtonPressed
 {
        WBAuthorizeRequest *request = [WBAuthorizeRequest request];
        request.redirectURI = kRedirectURI;
        request.scope = @"all";
        request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
                             @"Other_Info_1": [NSNumber numberWithInt:123],
                             @"Other_Info_2": @[@"obj1", @"obj2"],
                             @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
        [WeiboSDK sendRequest:request]; } 
    
    @end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值