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

1.在ShareSDk注册账号,并建立应用!获取到AppKey和AppSecret!html

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

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

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时触发,

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

     *  若是您使用的时服务端托管平台信息时,第2、四项参数能够传入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中,在按钮的点击事件中分别添加app

- (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,与以前有不少不一样的地方,接下来的这些设置很重要!ui

 

在URLType下添加对应的appIDspa

由于ios只支持https协议,因此,在info.plist添加以下类,使应用退回到http。code

 

这里也要修改,不然微博登陆会崩!orm

 

接近尾声了,接下来,在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>

 

而后就能够运行在本身的真机上了!

效果图:

 

 

 

相关文章
相关标签/搜索