本文實例為大家分享了iOS QQ第三方登錄實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
一、準備工作
1、到QQ開放平臺(http://connect.qq.com/ )注冊成為開發(fā)者,申請appkey,
2、在URL Types中添加QQ的AppID,其格式為:”tencent” + AppID 例如tencent1104463316
二、配置AppDelegate.m
1、導(dǎo)入<TencentOpenAPI/QQApiInterface.h> 和<TencentOpenAPI/TencentOAuth.h>兩個頭文件
2、配置函數(shù):
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [TencentOAuth HandleOpenURL:url]; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [TencentOAuth HandleOpenURL:url]; }
三、在登錄界面中設(shè)置代碼
1、導(dǎo)入#import <TencentOpenAPI/TencentOAuth.h>
2、遵守協(xié)議:<TencentSessionDelegate>
3、觸發(fā)登陸方法中寫入一下代碼:
-(void)QQLogin { NSArray* permissions = [NSArray arrayWithObjects: kOPEN_PERMISSION_GET_USER_INFO, kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, kOPEN_PERMISSION_ADD_ALBUM, nil nil]; [self.tencentOAuth authorize:permissions]; }
4、獲取用戶信息
-(void)tencentDidLogin { [self.tencentOAuth getUserInfo]; } [objc] view plain copy 在CODE上查看代碼片派生到我的代碼片- (void)getUserInfoResponse:(APIResponse*) response { NSLog(@"getUserInfo = %@",response.jsonResponse); NSLog(@"getUserInfo.nickName = %@",[response.jsonResponseobjectForKey:@"nickname"]); NSString* alertMessage = [NSStringstringWithFormat:@"%@",response.jsonResponse]; NSString* headerPath = [response.jsonResponseobjectForKey:@"figureurl_qq_2"]; NSString* name = [response.jsonResponseobjectForKey:@"nickname"]; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點
疑難解答