本文實例為大家分享了iOS QQ第三方登錄實現代碼,供大家參考,具體內容如下
一、准備工作
1、到QQ開放平台(http://connect.qq.com/ )注冊成為開發者,申請appkey,
2、在URL Types中添加QQ的AppID,其格式為:”tencent” + AppID 例如tencent1104463316
二、配置AppDelegate.m
1、導入<TencentOpenAPI/QQApiInterface.h> 和<TencentOpenAPI/TencentOAuth.h>兩個頭文件
2、配置函數:
- (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]; }
三、在登錄界面中設置代碼
1、導入#import <TencentOpenAPI/TencentOAuth.h>
2、遵守協議:<TencentSessionDelegate>
3、觸發登陸方法中寫入一下代碼:
-(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"]; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。