iOS开发----集成的QQ第三方登录

1. 集成的QQ登录 sdk下载网址


"http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD"  SDK下载页面下载最新版本QQ登录iOS SDK。

iOS开发----集成的QQ第三方登录_第1张图片

iOS SDK包中带有两个文件:

1. TencentOpenAPI.framework打包了iOS SDK的头文件定义和具体实现。

2. TencentOpenApi_iOS_Bundle.bundle 打包了iOS SDK需要的资源文件。


2.参照ios集成文档设置相关环境


1)将iOS SDK中的TencentOpenAPI.framework和TencentOpenApi_IOS_Bundle.bundle文件拷贝到应用开发的目录下。

iOS开发----集成的QQ第三方登录_第2张图片

在“Build Phases”中选择展开“Copy Bundle Resources”一栏,并点击“+”图标,添加SDK依赖的系统库文件。分别是”Security.framework”,“libiconv.dylib”,“SystemConfiguration.framework”,“CoreGraphics.Framework”、“libsqlite3.dylib”、“CoreTelephony.framework”、“libstdc++.dylib”、“libz.dylib”。

iOS开发----集成的QQ第三方登录_第3张图片

3.代码


在XCode中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“URL type”添加一条新的“URL scheme”,新的scheme = tencent + appid。如果您使用的是XCode3或者更低的版本,则需要在plist文件中添加。Demo中我们注册的appid是1105625767。

appID获得的详细步骤可以很举一下网址:http://jingyan.baidu.com/article/d2b1d10292eabc5c7e37d4b5.html

在AppDelegate.m文件中添加openURL的方法,当其他应用通过openURL:打开当前应用的时候会触发。

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{

return [TencentOAuth HandleOpenURL:url];

}

handleOpenURL:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{

return [TencentOAuth HandleOpenURL:url];

}

main.stroyboard中添加一个按钮,一个imageView,一个label,当登陆成功的时候,imageView会返回你的qq头像,label显示你的qq签名


iOS开发----集成的QQ第三方登录_第4张图片

在viewController.m中导入#import   #import

@interface ViewController (){

TencentOAuth *_tencentOAuth;

}

对按钮进行关联- (IBAction)qqLogin:(id)sender {

//创建一个授权对象 }

_tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"1105625767"   andDelegate:self];

//权限

NSArray* permissions = [NSArray arrayWithObjects:

kOPEN_PERMISSION_GET_USER_INFO,

kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,

kOPEN_PERMISSION_ADD_ALBUM,

kOPEN_PERMISSION_ADD_ONE_BLOG,

kOPEN_PERMISSION_ADD_SHARE,

kOPEN_PERMISSION_ADD_TOPIC,

kOPEN_PERMISSION_CHECK_PAGE_FANS,

kOPEN_PERMISSION_GET_INFO,

kOPEN_PERMISSION_GET_OTHER_INFO,

kOPEN_PERMISSION_LIST_ALBUM,

kOPEN_PERMISSION_UPLOAD_PIC,

kOPEN_PERMISSION_GET_VIP_INFO,

kOPEN_PERMISSION_GET_VIP_RICH_INFO,

nil];

//登录

[_tencentOAuth authorize:permissions inSafari:NO];

}

实现TencentSessionDelegate,TencentLoginDelegate的方法

#pragma mark - TencentSessionDelegate

- (void)getUserInfoResponse:(APIResponse*) response

{

NSString *imageUrl = response.jsonResponse[@"figureurl_qq_2"];

NSString *name = response.jsonResponse[@"nickname"];

//昵称

self.nicknameLabel.text = name;

//头像

dispatch_async(dispatch_get_global_queue(0, 0), ^{

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];

dispatch_async(dispatch_get_main_queue(), ^{

self.headImageView.image = [UIImage imageWithData:data];

});

});

}

#pragma mark - TencentLoginDelegate

- (void)tencentDidLogin

{

[[[UIAlertView alloc] initWithTitle:@"提示" message:@"登录成功" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];

//获取用户的昵称和头像---该方法自动获取accessToken,提交到腾讯服务器

[_tencentOAuth getUserInfo];

/*

登录成功后,即可获取到access token和openid。accessToken和 openid

*/

//凭证,用于后续访问各开放接口

[_tencentOAuth accessToken] ;

//用户授权登录后对该用户的唯一标识.最终openId提交到自己服务器

[_tencentOAuth openId] ;

}

/**

* 登录时网络有问题的回调

*/

- (void)tencentDidNotNetWork

{

[[[UIAlertView alloc] initWithTitle:@"提示" message:@"网络异常" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];

}

/**

* 登录失败后的回调

* \\param cancelled 代表用户是否主动退出登录

*/

- (void)tencentDidNotLogin:(BOOL)cancelled

{

if (cancelled)

{

[[[UIAlertView alloc] initWithTitle:@"提示" message:@"取消登录" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];

}

else

{

[[[UIAlertView alloc] initWithTitle:@"提示" message:@"登录失败" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];

}

}

点击QQ的登录按钮,实现跳转,进入qq登录界面,如下图

iOS开发----集成的QQ第三方登录_第5张图片

以上有借鉴部分,如发现问题及bug,可以留言,我们可以互相学习,相互探讨。

你可能感兴趣的:(iOS开发----集成的QQ第三方登录)