2 cuimingran cuimingran 于 2014.11.13 11:36 提问

跪求iOS开发的新浪微博授权登录,要求源码实现.

跪求iOS开发的新浪微博授权登录,要求源码实现,不要第三方的,也不要静态库的,求大神给个Demo,本人小菜鸟一枚,刚工作不久,求大神关照,无以为报,只愿好人一生平安!
QQ:903142826 邮箱cuimingran@163.com

3个回答

u013243469
u013243469   2014.11.19 18:55
已采纳

1.首先你要有新浪开发者账号,无非是到开放平台建立一个项目,然后把你的账号添加进去,你创建的项目中有app key,app secret以及授权跳转地址uri,还要在工程中导入新浪SDK
AppDelegate.h
#import "SinaWeibo.h"
#define kAppKey

#define kAppSecret

#define kAppRedirectURI
宏后面对应你的app key,app secret以及授权跳转地址uri
AppDelegate..m
//初始化新浪微博对象

  • (void)_initSinaWeibo
    {
    _sinaweibo = [[SinaWeibo alloc] initWithAppKey:kAppKey appSecret:kAppSecret appRedirectURI:kAppRedirectURI andDelegate:self];

    //读取本地保存的认证信息
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *sinaweiboInfo = [defaults objectForKey:@"SinaWeiboAuthData"];
    if ([sinaweiboInfo objectForKey:@"AccessTokenKey"] && [sinaweiboInfo objectForKey:@"ExpirationDateKey"] && [sinaweiboInfo objectForKey:@"UserIDKey"])
    {
    _sinaweibo.accessToken = [sinaweiboInfo objectForKey:@"AccessTokenKey"];
    _sinaweibo.expirationDate = [sinaweiboInfo objectForKey:@"ExpirationDateKey"];
    _sinaweibo.userID = [sinaweiboInfo objectForKey:@"UserIDKey"];

    NSLog(@"读取本地授权信息成功");
    

    }

}

#pragma mark -SinaWeibo delegate

  • (void)sinaweiboDidLogIn:(SinaWeibo *)sinaweibo { NSDictionary *authData = [NSDictionary dictionaryWithObjectsAndKeys: sinaweibo.accessToken, @"AccessTokenKey", sinaweibo.expirationDate, @"ExpirationDateKey", sinaweibo.userID, @"UserIDKey", sinaweibo.refreshToken, @"refresh_token", nil]; [[NSUserDefaults standardUserDefaults] setObject:authData forKey:@"SinaWeiboAuthData"]; [[NSUserDefaults standardUserDefaults] synchronize];

}
在你需要登录的视图控制器里面
写如下方法

  • (SinaWeibo *)sinaweibo { AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate; return delegate.sinaweibo; } 登陆的地方写,在相同视图控制器里面 //打开授权界面登陆 [self.sinaweibo logIn];

//注销账号
//删除新浪的认证信息
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"SinaWeiboAuthData"];
[[NSUserDefaults standardUserDefaults] synchronize];

save4me
save4me   Ds   Rxr 2014.11.19 13:43
save4me
save4me   Ds   Rxr 2014.12.18 10:39

上面的答案有帮助吗?如果还有问题,请提出来,如果对答案满意,请顶一下,并标记为采纳答案,谢谢!

Csdn user default icon
上传中...
上传图片
插入图片