第三方授权登陆

我不写代码 , 我只是代码的搬运工

往工程中添加一个照片 命名为 1.png 放到Assets.xcassets里面
添加 : 网络授权
官方文档在这里
在官网获取appkey点击这里
获取ShareSDK-iOS (和微博分享用的是一个SDK)点击这里
下载完SDK , 拖拽到我们的工程里

添加依赖库

必须添加的依赖库如下(Xcode 7 之后 .dylib库后缀名更改为.tbd):

libicucore.tbd
libz.tbd
 libstdc++.dylib (这个库在XCode10之后找不到,已经被废弃,以libc++替代)
JavaScriptCore.framework

以下依赖库根据社交平台添加:

新浪微博SDK依赖库
ImageIO.framework
libsqlite3.dylib

QQ好友和QQ空间SDK依赖库
libsqlite3.dylib

微信SDK依赖库
libsqlite3.dylib

Instagram需要依赖库
AssetsLibrary.framework
Photos.framework

美拍需要依赖库
AssetsLibrary.framework

设置ShareSDK的Appkey并初始化对应的第三方社交平台
(在info.plist里添加ShareSDK的Appkey并且为之设置相应的ShareSDK的Appkey) 如图
在这里插入图片描述
打开*AppDelegate.m导入头文件

#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"
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!新浪微博SDK需要在项目Build Settings中的Other Linker Flags添加"-ObjC"
 
//人人SDK头文件
#import <RennSDK/RennSDK.h>

在 “”““”“ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ ””“”“”“ 中(appledelegate.m中)调用以下

[ShareSDK registerActivePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            @(SSDKPlatformTypeMail),
                            @(SSDKPlatformTypeSMS),
                            @(SSDKPlatformTypeCopy),
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ),
                            @(SSDKPlatformTypeRenren),
                            @(SSDKPlatformTypeFacebook),
                            @(SSDKPlatformTypeTwitter),
                            @(SSDKPlatformTypeGooglePlus),
]
           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;
                  case SSDKPlatformTypeRenren:
                          [ShareSDKConnector connectRenren:[RennClient class]];
                    break;
                  default:
                    break;
                }
          }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
          {
 
              switch (platformType)
              {
                  case SSDKPlatformTypeSinaWeibo:
                      //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权
                      [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243"
                                                appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                                              redirectUri:@"http://www.sharesdk.cn"
                                                 authType:SSDKAuthTypeBoth];
                   break;
                  case SSDKPlatformTypeWechat:
                      [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885"
                                            appSecret:@"64020361b8ec4c99936c0e3999a9f249"];
                   break;
                  case SSDKPlatformTypeQQ:
                      [appInfo SSDKSetupQQByAppId:@"100371282"
                                           appKey:@"aed9b0303e3ed1e27bae87c33761161d"
                                         authType:SSDKAuthTypeBoth];
                   break;
                   //如果人人视频报错直接删掉就好了  从  case 删除到 break
                  case SSDKPlatformTypeRenren:
                      [appInfo        SSDKSetupRenRenByAppId:@"226427"
                                               appKey:@"fc5b8aed373c4c27a05b712acba0f8c3"
                                            secretKey:@"f29df781abdd4f49beca5a2194676ca4"
                                             authType:SSDKAuthTypeBoth];
                    break;
                  case SSDKPlatformTypeFacebook:
                      [appInfo SSDKSetupFacebookByApiKey:@"107704292745179"
                                               appSecret:@"38053202e1a5fe26c80c753071f0b573"
                                            displayName:@"shareSDK"
                                             authType:SSDKAuthTypeBoth];
                    break;
                  case SSDKPlatformTypeTwitter:
                      [appInfo SSDKSetupTwitterByConsumerKey:@"LRBM0H75rWrU9gNHvlEAA2aOy"
                                               consumerSecret:@"gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G"
                                            redirectUri:@"http://mob.com"]
                    break;
                   case SSDKPlatformTypeGooglePlus:
                     [appInfo SSDKSetupGooglePlusByClientID:@"232554794995.apps.googleusercontent.com"
                                                clientSecret:@"PEdFgtrMw97aCvf0joQj7EMk"
                                                 redirectUri:@"http://localhost"];
                    break;
                   default:
                    break;
              }
          }];
return YES;
}

在ViewController.m里面来做Ui和点击登陆的按钮事件
先导入头文件

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>

创建三个按钮 分别是 微博登陆 , 微信登陆 , qq 登陆 的按钮

@interface ViewController ()
{
    UIButton *wbbtn;
    UIButton *wxbtn;
    UIButton *qqbtn;
}

为三个按钮设置位置 , 点击事件等

- (void)viewDidLoad {
    [super viewDidLoad];
    wbbtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 80, 80)];
    [wbbtn setTitle:@"登陆微博" forState:UIControlStateNormal];
    [wbbtn setBackgroundColor:[UIColor redColor]];
    [wbbtn addTarget:self action:@selector(weibo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wbbtn];
    
    wxbtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 80, 80)];
    [wxbtn setTitle:@"登陆微信" forState:UIControlStateNormal];
    [wxbtn setBackgroundColor:[UIColor redColor]];
    [wxbtn addTarget:self action:@selector(weixin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wxbtn];
    
    qqbtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 300, 80, 80)];
    [qqbtn setTitle:@"登陆QQ" forState:UIControlStateNormal];
    [qqbtn setBackgroundColor:[UIColor redColor]];
    [qqbtn addTarget:self action:@selector(qq) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:qqbtn];
}

微博的点击方法 ( 由于微博登陆 , 我也不知道咋回事 反正就是出不来 , 只好利用分享的方法进行登陆微博) 代码如下

- (void)weibo{
    NSArray* imageArray = @[[UIImage imageNamed:@"1.png"]];
    if (imageArray) {

        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:@"分享内容"
                                         images:imageArray
                                            url:[NSURL URLWithString:@"http://mob.com"]
                                          title:@"分享标题"
                                           type:SSDKContentTypeAuto];
        //有的平台要客户端分享需要加此方法,例如微博
        [shareParams SSDKEnableUseClientShare];
        //2、分享(可以弹出我们的分享菜单和编辑界面)
        [ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图,只有传这个才可以弹出我们的分享菜单,可以传分享的按钮对象或者自己创建小的view 对象,iPhone可以传nil不会影响
                                 items:nil
                           shareParams:shareParams
                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {

                       switch (state) {
                           case SSDKResponseStateSuccess:
                           {
                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                                   message:nil
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"确定"
                                                                         otherButtonTitles:nil];
                               [alertView show];
                               break;
                           }
                           case SSDKResponseStateFail:
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:[NSString stringWithFormat:@"%@",error]
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"OK"
                                                                     otherButtonTitles:nil, nil];
                               [alert show];
                               break;
                           }
                           default:
                               break;
                       }
                   }
         ];}
}

微博比较特殊 所以我列出来 ,而 QQ 微信 举一反三 , 所以我只举一个例子
以微信为例
微信的点击方法 请注意报错的地方

- (void)weixin{
		下一行中     SSDKPlatformTypeWechat      代表是微信的登陆 , QQ就写QQ的就好了  ,  别担心找不到在哪   我会在代码下面列出来的!!!
    [ShareSDK authorize:       SSDKPlatformTypeWechat         settings:nil onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        if (state == SSDKResponseStateSuccess)
        {
            NSLog(@"%@",user.rawData);
            NSLog(@"uid===%@",user.uid);
            NSLog(@"%@",user.credential);
        }
        else if (state == SSDKResponseStateCancel)
        {
            NSLog(@"取消");
        }
        else if (state == SSDKResponseStateFail)
        {
            NSLog(@"%@",error);
        }
    }];
}


/*
SSDKPlatformTypeSinaWeibo      			  微博
SSDKPlatformTypeMail					Mail  我也不知道是啥软件
SSDKPlatformTypeSMS              		 SMS  我也不知道是啥软件
SSDKPlatformTypeCopy            		 Copy 我也不知道是啥软件
SSDKPlatformTypeWechat      		 	  这个是微信
SSDKPlatformTypeQQ						QQ
SSDKPlatformTypeRenren				人人视频
SSDKPlatformTypeFacebook			脸书 (这个做了也没用 , 国外软件需要翻墙)
SSDKPlatformTypeTwitter				推特 (这个做了也没用 , 国外软件需要翻墙)
SSDKPlatformTypeGooglePlus		谷歌 (这个做了也没用 , 国外软件需要翻墙)


*/

还剩最重要的一点!!!
在这里插入图片描述
第六步填什么??
看这里 我只列举一部分 (官方文档比较全面)
一些appkey 我们申请不到 所以直接用官方给的

新浪微博	wb+微博appKey	如AppKey:568898243 wb568898243
所以 :  新浪微博添     wb568898243

QQ/Qzone	“tencent”+腾讯QQ互联应用appID	如appID:100371282 tencent100371282
所以 : QQ添   tencent100371282

微信	微信的appid	wx4868b35061f87885
所以 : 微信添 : wx4868b35061f87885

举个例子 :
在这里插入图片描述
最后的最后 command+r 运行!

猜你喜欢

转载自blog.csdn.net/weixin_42925415/article/details/82770836