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

 

然后就可以運行在自己的真機上了!

效果圖:

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM