ios 微博第三方登入及得到相關引數
阿新 • • 發佈:2019-02-18
1.實現微博第三方登入,首先要在”新浪開發者平臺”註冊,登入。
http://open.weibo.com
2.建立應用:
微連線->立即建立微連線->移動應用->填寫資料(教程可以去以下網址下載)
https://github.com/sinaweibosdk/weibo_ios_sdk
3.下載完成後,看 “微博iOS平臺SDK文件V3.1.1.pdf” 這個文件。文件中有如何教你配置檔案,但是WeiboSDK支援使用Cocoapods整合,請在Podfile中新增以下語句:
pod "WeiboSDK", :git => "https://github.com/sinaweibosdk/weibo_ios_sdk.git"
跟著”微博iOS平臺SDK文件V3.1.1.pdf” 這個文件,完成”一.SDK接入設定”中的 1,2,3,4,8,9,10
至此,檔案配置完成。
4.我們需要做的是第三方登入,即: “認證授權”
看”二.應用場景程式碼示例”中的 “1.SSO微部落格戶端授權認證”。
5.微博API文件
http://sinaweibosdk.github.io/weibo_ios_sdk/index.html
WeiboSDK->sendRequest->Discussion
( 請求傳送給微部落格戶端程式之後,微部落格戶端程式會進行相關的處理,處理完成之後一定會呼叫 [WeiboSDKDelegate didReceiveWeiboResponse:] 方法將處理結果返回給第三方應用)
所以在APPDelegate中,我們還需要遵循 “WeiboSDKDelegate”協議,這兩個協議中有兩個必須實現的方法,分別是:
didReceiveWeiboRequest:
didReceiveWeiboResponse:
//以下是demo中的示例:
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
}
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
if ([response isKindOfClass:WBSendMessageToWeiboResponse.class ])
{
NSString *title = NSLocalizedString(@"傳送結果", nil);
NSString *message = [NSString stringWithFormat:@"%@: %d\n%@: %@\n%@: %@", NSLocalizedString(@"響應狀態", nil), (int)response.statusCode, 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];
WBSendMessageToWeiboResponse* sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;
NSString* accessToken = [sendMessageToWeiboResponse.authResponse accessToken];
if (accessToken)
{
self.wbtoken = accessToken;
}
NSString* userID = [sendMessageToWeiboResponse.authResponse userID];
if (userID) {
self.wbCurrentUserID = userID;
}
[alert show];
}
else 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];
}
else if ([response isKindOfClass:WBPaymentResponse.class])
{
NSString *title = NSLocalizedString(@"支付結果", nil);
NSString *message = [NSString stringWithFormat:@"%@: %d\nresponse.payStatusCode: %@\nresponse.payStatusMessage: %@\n%@: %@\n%@: %@", NSLocalizedString(@"響應狀態", nil), (int)response.statusCode,[(WBPaymentResponse *)response payStatusCode], [(WBPaymentResponse *)response payStatusMessage], 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];
[alert show];
}
else if([response isKindOfClass:WBSDKAppRecommendResponse.class])
{
NSString *title = NSLocalizedString(@"邀請結果", nil);
NSString *message = [NSString stringWithFormat:@"accesstoken:\n%@\nresponse.StatusCode: %d\n響應UserInfo資料:%@\n原請求UserInfo資料:%@",[(WBSDKAppRecommendResponse *)response accessToken],(int)response.statusCode,response.userInfo,response.requestUserInfo];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"確定", nil)
otherButtonTitles:nil];
[alert show];
}
}
至此,我們已經實現了第三方登入了!
但是,怎麼才能獲得相關的資料呢?
//參考如下:
AppDelegate * appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSMutableDictionary * param = [[NSMutableDictionary alloc]initWithCapacity:2];
[param setObject:appdelegate.wbtoken forKey:@"access_token"];
[param setObject:appdelegate.wbCurrentUserID forKey:@"uid"];
NSString * userInfoUrl = @"https://api.weibo.com/2/users/show.json";
[WBHttpRequest requestWithAccessToken:appdelegate.wbtoken url:userInfoUrl httpMethod:@"GET" params:param delegate:self withTag:@"userInfo"];
- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data
{
//此處不需要使用匯入第三方SBJson去解析data也可以
id d = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSDictionary * dict = (NSDictionary *)d;
// NSLog(@"dict:%@",dict);
nameStr = [dict objectForKey:@"name"];
imageUrlStr = [dict objectForKey:@"profile_image_url"];
NSLog(@"name:%@,urlStr:%@",nameStr,imageUrlStr);
}