重点内容
详细集成微博登录功能的步骤如下:
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