iOS QQ第三方授權登錄(cocoa pods)

cocoa pods集成
(寫一句代碼都是罪過)
騰訊開發品臺
創建一個項目(做demo不需要提交審核)
info -> URL Types 設置回調 URL Schemes
identifier: tencentopenapi
URL: Schemes tencentAPPID

WechatIMG1.jpeg

設置白名單(QQ全面的白名單)

<key>LSApplicationQueriesSchemes</key>
 <array>
  <string>mqqapi</string>
  <string>mqq</string>
  <string>mqqOpensdkSSoLogin</string>
  <string>mqqconnect</string>
  <string>mqqopensdkdataline</string>
  <string>mqqopensdkgrouptribeshare</string>
  <string>mqqopensdkfriend</string>
  <string>mqqopensdkapi</string>
  <string>mqqopensdkapiV2</string>
  <string>mqqopensdkapiV3</string>
  <string>mqzoneopensdk</string>
  <string>mqqopensdkapiV3</string>
  <string>mqqopensdkapiV3</string>
  <string>mqzone</string>
  <string>mqzonev2</string>
  <string>mqzoneshare</string>
  <string>wtloginqzone</string>
  <string>mqzonewx</string>
  <string>mqzoneopensdkapiV2</string>
  <string>mqzoneopensdkapi19</string>
  <string>mqzoneopensdkapi</string>
  <string>mqzoneopensdk</string>
 </array>

設置ATS

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
#import "AppDelegate.h"
#import <TencentOpenAPI/TencentOAuth.h>

@interface AppDelegate ()<TencentSessionDelegate>
///TencentOAuth *tencentOAuth
@property(nonatomic,strong)TencentOAuth *tencentOAuth;
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"1105767298" andDelegate:self];
    NSArray *permissions = [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_INFO, kOPEN_PERMISSION_GET_USER_INFO, kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, nil];

    [_tencentOAuth authorize:permissions];

    return YES;
}
//登錄成功:
- (void)tencentDidLogin
{
    if (_tencentOAuth.accessToken.length > 0) {
        // 獲取用戶信息
        [_tencentOAuth getUserInfo];
        NSLog(@"_accessToken==%@",_tencentOAuth.accessToken);
        
    } else {
        NSLog(@"登錄不成功 沒有獲取accesstoken");
    }
}
// 獲取用戶信息
- (void)getUserInfoResponse:(APIResponse *)response {
    
    if (response && response.retCode == URLREQUEST_SUCCEED) {
        
        NSDictionary *userInfo = [response jsonResponse];
        NSString *nickName = userInfo[@"nickname"];
        NSLog(@"userInfo==%@",userInfo);
        NSLog(@"nicName==%@",nickName);
        // 后續操作...
        
        
    } else {
        NSLog(@"QQ auth fail ,getUserInfoResponse:%d", response.detailRetCode);
    }
}
//非網絡錯誤導致登錄失敗:
- (void)tencentDidNotLogin:(BOOL)cancelled {
    if (cancelled) {
        NSLog(@"用戶取消登錄");
    } else {
        NSLog(@"登錄失敗");
    }
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
    return [TencentOAuth HandleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    return [TencentOAuth HandleOpenURL:url];
}
@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容