在進行SSO請求以前 咱們要先去新浪微博的開放平臺http://open.weibo.com/進行建立應用.以便獲得appKey 和AppSecret.json
點擊建立應用 .進行資料填寫 在這裏api
Apple ID 是如今能夠隨意填寫的 可是在正式應用上線後 須要立刻更改app
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 .url
[WBHttpRequest requestWithAccessToken:kDefineToken url:@"https://api.weibo.com/2/statuses/public_timeline.json" httpMethod:@"GET" params:nil delegate:self withTag:nil];
spa