ShareSDK第三方登录

1.集成微信

(本篇默认以未集成分享功能进行讲解):
首先在AppDelegate中:
(1).导入以下头文件


#import 
#import 
//腾讯开放平台(对应QQ和QQ空间)SDK头文件
#import 
#import 

//微信SDK头文件
#import "WXApi.h"

//新浪微博SDK头文件
#import "WeiboSDK.h"

(2).在didFinishLaunchingWithOptions:方法中写入下面的代码,并更改成自己对应的参数

    [ShareSDK registerApp:@"xxxxxx"

     
          activePlatforms:@[
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ),
                            @(SSDKPlatformTypeSinaWeibo)]
                 onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeWechat:
                 [ShareSDKConnector connectWeChat:[WXApi class]];
                 break;
             case SSDKPlatformTypeQQ:
                 [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                 break;
             case SSDKPlatformTypeSinaWeibo:
                 [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                 break;
             default:
                 break;
         }
     }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeSinaWeibo://暂未申请(无法申请)
                 //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权
                 [appInfo SSDKSetupSinaWeiboByAppKey:@"xxxxxx"
                                           appSecret:@"xxxxxx"
                                         redirectUri:@"xxxxxx"
                                            authType:SSDKAuthTypeBoth];
                 break;

             case SSDKPlatformTypeWechat:
                 [appInfo SSDKSetupWeChatByAppId:@"xxxxxx"
                                       appSecret:@"xxxxxx"];
                 break;
             case SSDKPlatformTypeQQ:
                 [appInfo SSDKSetupQQByAppId:@"xxxxxx"
                                      appKey:@"xxxxxx"
                                    authType:SSDKAuthTypeBoth];
                 break;

             default:
                 break;
         }

     }];

以及实现下面的方法

- (BOOL)application:(UIApplication *)application
      handleOpenURL:(NSURL *)url
{
    [LDSDKManager handleOpenURL:url];
    return [WXApi handleOpenURL:url delegate:self];
}


-(BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation {
    
    [LDSDKManager handleOpenURL:url];
    return [WXApi handleOpenURL:url delegate:self];
    
}

(3).设置urlScheme和info.plist 中加入白名单
同分享的设置

(4).在需要实现登录的地方
同样导入头文件:

#import 
#import 
#import "WXApi.h"

写下如下的代码:

实现登录的方法


[ShareSDK getUserInfo:SSDKPlatformTypeWechat
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             CMWechatUser *wechatUser =[CMWechatUser wechatInit];
             // 初始化数据
             wechatUser =  [wechatUser initWithWechatUser:user];
             
             //保存到本地
             [CMUserTools saveWechatAccount:wechatUser];
             
             [ws requestAllUserCountData:wechatUser];
             
         }
         
         else
         {
             if (![WXApi isWXAppInstalled]) {
                 [DisplayHelper displayWarningAlert:@"您还没有安装微信哦!"];
                 return ;
             }
             [DisplayHelper displayWarningAlert:@"微信登录验证失败"];
             NSLog(@"%@",error);
         }
         
     }];

下面是取消授权,也就是退出微信登录的方法

-(void)cancleLoginUser {
    
    [ShareSDK cancelAuthorize:SSDKPlatformTypeWechat];
    WS(ws);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [ws changeLoginState:[ShareSDK hasAuthorized:SSDKPlatformTypeWechat]];
        if ([ShareSDK hasAuthorized:SSDKPlatformTypeWechat]) {//授权成功
            [DisplayHelper displayWarningAlert:@"微信授权退出失败,请稍后重试"];
        }else {// 取消授权成功
            [CMUserTools removeAllWechatAndUserDatas];
            [DisplayHelper displaySuccessAlert:@"微信授权退出成功!"];
        }
    });    
}

注意点:

1.微信登录申请的开发平台必须是审核通过的,也就是必须掏300元通过微信审核过的账号才可以用的
2.由于微信注销,也就是退出微信登录的注销成功与否,没有回调,所以模拟注销的过程可以延时一定的时间,然后用[ShareSDK hasAuthorized:SSDKPlatformTypeWechat]来判断是否注销成功
3.微信的账号授权成功后与SSDKUser的里面是user的所有属性,ShareSDK解析后的为uid的属性可以唯一标示该微信用户,可以作为用户名给后台设置

你可能感兴趣的:(ShareSDK第三方登录)