使用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>

 

而後就能夠運行在本身的真機上了!

效果圖:

 

 

 

相關文章
相關標籤/搜索