您的位置:首页 > 移动开发 > IOS开发

iOS 集成第三方微博登录功能

2015-12-15 12:06 483 查看
重点内容详细集成微博登录功能的步骤如下:重点内容

1.先到官网下载微博SDK

2.将其导入你要集成的工程中

3.导入相关的系统依赖库即可

import “AppDelegate.h”

@interface AppDelegate ()

@end

@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);

// [CYUCPassportLib loginWithOtherPlarform:CYUC_OTHER_PLATFORM_WEIBO

// uid:uid

// nickName:nickName synchronous:YES

// completionHandler:^(NSDictionary *result) {

// NSDictionary *dicNotificationUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:@(CYUC_RETURN_APP_STATU_SUCCESS), CYUC_RETURN_APP_DICTIONARY_KEY_STATU, [CYUCManager sharedInstance].userInfo, @”userinfo”, nil];

// [[NSNotificationCenter defaultCenter] postNotificationName:CYUC_NOTIFICATION_USER_INFO object:dictionary userInfo:dicNotificationUserInfo];

//

// NSLog(@”%@”, result);

// }];

}

- (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];

}

import ViewController

@interface ViewController : UIViewController

@end

@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)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

- (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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: