在进行SSO请求之前 我们要先去新浪微博的开放平台http://open.weibo.com/进行创建应用.以便得到appKey 和AppSecret.
点击创建应用 .进行资料填写 在这里
Apple ID 是现在可以随意填写的 但是在正式应用上线后 需要马上更改
Bundle ID 必须要和Xcode上的 Bundle Identifier 上的一样.1) 导入libWeiboSDK设置代理.WeiboSDKDelegate2)注册Appkey
[WeiboSDK enableDebugMode:YES];
[WeiboSDK registerApp:kAppKey];
3)创建一个Button用来触发sso请求//创建一个Button来点击请求授权
_ssoButton=[UIButton buttonWithType:UIButtonTypeCustom];
// [_ssoButton setTitle:@"hehehehe" forState:UIControlStateNormal];//要颜色啊
[_ssoButton setImage:[UIImage imageNamed:@"LoginButton@2x.png"] forState:UIControlStateNormal];
[_ssoButton addTarget:self action:@selector(ssoButtonRequest) forControlEvents:UIControlEventTouchDown];
[_ssoButton setFrame:CGRectMake(100, 350, 130, 80)];
[self.view addSubview:_ssoButton];
4)触发 发送授权请求-(void)ssoButtonRequest
{
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];
}5)接受weibo的响应
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
{
NSString *title = @"发送结果";
NSString *message = [NSString stringWithFormat:@"响应状态: %d\n响应UserInfo数据: %@\n原请求UserInfo数据: %@",
response.statusCode, response.userInfo, response.requestUserInfo];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
}
else if ([response isKindOfClass:WBAuthorizeResponse.class])
{
NSString *title = @"认证结果";
NSString *message = [NSString stringWithFormat:@"响应状态: %d\nresponse.userId: %@\nresponse.accessToken: %@\n响应UserInfo数据: %@\n原请求UserInfo数据: %@",
response.statusCode, [(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken], response.userInfo, response.requestUserInfo];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
self.wbtoken = [(WBAuthorizeResponse *)response accessToken];
NSLog(@"token=%@", self.wbtoken);
// 保存授权用户的token 如果需要进行API请求 便要保存密钥
[[NSUserDefaults standardUserDefaults] setObject:self.wbtoken forKey:@"access_token"];
[alert show];
[alert release];
}
}
6)创建一个按钮 触发请求接口数据 (这里以请求200条公共微博为例子.默认20条)
UIButton*requestDataButton=[UIButton buttonWithType:UIButtonTypeCustom];
[requestDataButton setTitle:@"请求数据" forState:UIControlStateNormal];
[requestDataButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[requestDataButton setBackgroundColor:[UIColor orangeColor]];
[requestDataButton addTarget:self action:@selector(requestData) forControlEvents:UIControlEventTouchDown];
[requestDataButton setFrame:CGRectMake(100, 420, 130, 80)];
[self.view addSubview:requestDataButton];
7)用异步请求去请求并且解析 用到了SBJson第三方
-(void)requestData
{
//创建密钥的字符串
NSString*access_token=[[NSUserDefaults standardUserDefaults]objectForKey:@"access_token"];
//创建一个URL...
NSURL*url=[NSURL URLWithString:[NSString stringWithFormat:@"https://api.weibo.com/2/statuses/public_timeline.json?access_token=%@",access_token]];
//创立一个request对象
NSURLRequest*request=[NSURLRequest requestWithURL:url];
//实例化一个可变的data 用来接受数据
_data=[[NSMutableData alloc]init];
//设置一个异步连接请求
[NSURLConnection connectionWithRequest:request delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"hehhe");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//进行拼接
[_data appendData:data];
NSLog(@"=========%d",_data.length);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString*dataStr=[[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];
NSDictionary*dataJS=[dataStr JSONValue ];
NSLog(@"datajs=%@",dataJS);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"有错误");
}
8)/********
重写AppDelegate 的 handleOpenURL 和 openURL 方法
*********/
//写上面即可...
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WeiboSDK handleOpenURL:url delegate:self];
}
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [WeiboSDK handleOpenURL:url delegate:self];
}
//这个sdk里面的请求是调用了手机内的微博app .
[WBHttpRequest requestWithAccessToken:kDefineToken url:@"https://api.weibo.com/2/statuses/public_timeline.json" httpMethod:@"GET" params:nil delegate:self withTag:nil];