使用ShareSDK实现QQ,微信,新浪微博的第三方登录

1.在ShareSDk注册帐号,并创建应用!获取到AppKey和AppSecret!

2.到QQ,微信,新浪微博开发者平台注册帐号,并创建应用,获取AppID和AppSecret。

3.在ShareSDK官网下载SDk包,并将其集成到自己的工程中。

4.根据官方文档,进行操作。

 

①在AppDelegate中导入

#import <ShareSDK/ShareSDK.h>

#import <ShareSDKConnector/ShareSDKConnector.h>

 

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

#import <TencentOpenAPI/TencentOAuth.h>

#import <TencentOpenAPI/QQApiInterface.h>

 

//微信SDK头文件

#import "WXApi.h"

 

//新浪微博SDK头文件

#import "WeiboSDK.h"

②在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中注册应用。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   

    /**

     *  设置ShareSDK的appKey,如果尚未在ShareSDK官网注册过App,请移步到http://mob.com/login 登录后台进行应用注册,

     *  在将生成的AppKey传入到此方法中。

     *  方法中的第二个第三个参数为需要连接社交平台SDK时触发,

     *  在此事件中写入连接代码。第四个参数则为配置本地社交平台时触发,根据返回的平台类型来配置平台信息。

     *  如果您使用的时服务端托管平台信息时,第二、四项参数可以传入nil,第三项参数则根据服务端托管平台来决定要连接的社交SDK。

     */

    [ShareSDK registerApp:@"ShareSDKAppKey"

     

          activePlatforms:@[

                            @(SSDKPlatformTypeSinaWeibo),

                            @(SSDKPlatformTypeWechat),

                            @(SSDKPlatformTypeQQ),

                            ]

                 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:@"AppKey"

                                           appSecret:@"appSecret"

                                         redirectUri:@"https://api.weibo.com/oauth2/default.html"

                                            authType:SSDKAuthTypeBoth];

                 break;

                 

                 //设置微信应用信息

             case SSDKPlatformTypeWechat:

                 [appInfo SSDKSetupWeChatByAppId:@"AppId"

                                       appSecret:@"appSecret"];

                 break;

                 

                //设置QQ应用信息,其中authType设置为使用SSO+Web形式授权

             case SSDKPlatformTypeQQ:

                 [appInfo SSDKSetupQQByAppId:@"AppId"

                                      appKey:@"appKey"

                                    authType:SSDKAuthTypeBoth];

                 break;

             

             default:

                 break;

         }

     }];

    

    

    self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    

    LoginViewController *loginVC=[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];

    UINavigationController *NC=[[UINavigationController alloc] initWithRootViewController:loginVC];

    self.window.rootViewController=NC;

    [_window makeKeyAndVisible];

    return YES;

}

③在LoginViewController.m中,在按钮的点击事件中分别添加

- (IBAction)didiClickQQ:(id)sender {

    

    //QQ的登录

    [ShareSDK getUserInfo:SSDKPlatformTypeQQ

           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)

     {

         if (state == SSDKResponseStateSuccess)

         {

             

             NSLog(@"uid=%@",user.uid);

             NSLog(@"%@",user.credential);

             NSLog(@"token=%@",user.credential.token);

             NSLog(@"nickname=%@",user.nickname);

         }

         

         else

         {

             NSLog(@"%@",error);

         }

         

     }];

    

}

 

/**

 *  微信

 *

 *  @param sender <#sender description#>

 */

- (IBAction)didClickWeChat:(id)sender {

    

    //微信的登录

    [ShareSDK getUserInfo:SSDKPlatformTypeWechat

           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)

     {

         if (state == SSDKResponseStateSuccess)

         {

             

             NSLog(@"uid=%@",user.uid);

             NSLog(@"%@",user.credential);

             NSLog(@"token=%@",user.credential.token);

             NSLog(@"nickname=%@",user.nickname);

         }

         

         else

         {

             NSLog(@"%@",error);

         }

         

     }];

   

    

    

}

 

/**

 *  微博

 *

 *  @param sender <#sender description#>

 */

 

- (IBAction)didClickWeiBo:(id)sender {

    

    //微博的登录

    [ShareSDK getUserInfo:SSDKPlatformTypeSinaWeibo

           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)

     {

         if (state == SSDKResponseStateSuccess)

         {

             

             NSLog(@"uid=%@",user.uid);

             NSLog(@"%@",user.credential);

             NSLog(@"token=%@",user.credential.token);

             NSLog(@"nickname=%@",user.nickname);

         }

         

         else

         {

             NSLog(@"%@",error);

         }

         

     }];

 

    

}

 

好了,到这里,官方文档上给的内容就全部添加完毕了。接下来运行,发现可以进入到qq,微信,但是输入对应的帐号,却不能正常登录。而微博直接就崩掉了。

由于现在更新到iOS9,与之前有很多不同的地方,接下来的这些设置很重要!

 

在URLType下添加对应的appID

因为ios只支持https协议,所以,在info.plist添加如下类,使应用退回到http。

 

这里也要修改,否则微博登录会崩!

 

接近尾声了,接下来,在QQ,微信,微博开发者平台里添加测试帐号。

 

忘了一步,添加白名单

<key>LSApplicationQueriesSchemes</key>
<array>
<string>mqqOpensdkSSoLogin</string>
<string>sinaweibo</string>
<string>mqq</string>
<string>mqqapi</string>
<string>mqqopensdkapiV3</string>
<string>mqqopensdkapiV2</string>
<string>mqqapiwallet</string>
<string>mqqwpa</string>
<string>mqqbrowser</string>
<string>weixin</string>
<string>wechat</string>
</array>

然后就可以运行在自己的真机上了!

效果图:

 

 

 

 

 

 

 

 

 

 

一个人,一片天,一条路,一瞬间!
原文地址:https://www.cnblogs.com/zcl410/p/4942643.html