iOS 集成第三方微博登录功能
2015-12-15 12:06
483 查看
重点内容详细集成微博登录功能的步骤如下:重点内容
1.先到官网下载微博SDK
2.将其导入你要集成的工程中
3.导入相关的系统依赖库即可
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
[WeiboSDK enableDebugMode:YES];
[WeiboSDK registerApp:kAppKey];
}
}
- (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];
(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];
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)ssoButtonPressed{
}
@end
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
相关文章推荐
- 适配iOS 9系统
- iOS中Bitcode的介绍及配置
- iOS xcode实用插件
- ios7自定义返回按钮后,右滑返回功能失效解决方法
- ios 自动生成 @2X 和 @3X 图 Xcode插件 RTImageAssets
- iOS开发 - 登陆post请求遇到的问题
- [iOS]iOS开发者账号申请与注册流程
- iOS CocoaPods安装和使用图解
- iOS QQ分享(最新2015.12.25)
- [Xcode 7.2]Xcode菜单栏之你不知道的那点事
- iOS开发之JSON和XML
- iOS--NSThread
- iOS-NSUserDefaults
- iOS tableView 改变头视图和脚视图的颜色
- IOS 开源框架:SDWebImage
- IOS中使用像素位图(CGImageRef)对图片进行处理
- iOS 返回父视图之后如何刷新页面
- iOS 学习日志 :开发中常用的注释方法以及代码块
- [iOS]iOS的字号转换问题
- iOS 开发获取当前时间 NSDate