美文网首页
iOS - 第三方分享和登陆 SSO(免登录)

iOS - 第三方分享和登陆 SSO(免登录)

作者: Mn_Su | 来源:发表于2016-09-23 13:21 被阅读0次

    第一. Mob版

     **一.登陆 mob 官网创建应用**
    
    [ **二.快速集成** ](http://wiki.mob.com/%e5%bf%ab%e9%80%9f%e9%9b%86%e6%88%90/)
    
        集成注意点:
    
            1.依赖库按照文档一一添加;
    
            2.手动集成版本要分版本,Xcode9 之前和之后;
    
                XCode9之前版本直接拖SDk进去请务必在上述步骤中选择“Create groups for any added folders”单选按钮组。如果你选择“Create folder references for any added folders”,一个蓝色的文件夹引用将被添加到项目并且将无法找到它的资源
    
            3.在项目工程的Info.plist 中如图增加 MOBAppKey 和 MOBAppSecret 两个字段;
    
            4.info.plist中设置[ 白名单 ](http://wiki.mob.com/%e9%80%82%e9%85%8dios-9%ef%bc%8b%e7%b3%bb%e7%bb%9f/)
    
            5.添加 URL Types ;
    
            
    
    **三.自定义分享菜单**
            1.[ 自定义UI  ](http://wiki.mob.com/%e8%87%aa%e5%ae%9a%e4%b9%89ui-2/)           
    
            2.无UI  
            //创建分享参数
            NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
            [shareParams SSDKSetupShareParamsByText:content
                                             images:image //传入要分享的图片
                                                url:[NSURL URLWithString:@"http://itunes.apple.com/cn/app/id1313283200?mt=8"]
                                              title:title
                                               type:SSDKContentTypeAuto];
            
            //进行分享
            [ShareSDK share:type //传入分享的平台类型
                 parameters:shareParams
             onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { // 回调处理....}];
                 switch (state) {
                     case SSDKResponseStateSuccess:
                         NSLog(@"分享成功!");
                         break;
                     case SSDKResponseStateFail:
                         NSLog(@"分享失败%@",error);
                         if (error.code == 208) {
                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"您当前手机尚未安装相关APP应用" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                             [alert show];
                             
                         }
                         
                         break;
                     case SSDKResponseStateCancel:
                         NSLog(@"分享已取消");
                         break;
                     default:
                         break;
                 }
             }];
    
    **四.代码相关**
    
            1.在AppDelegate 中导入相关头文件 ,在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  方法中导入下面代码
    
            - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
                [self  registShareSDKInAppDelegate];
            }            
    
                /* ShareSDK 相关 */
            - (void)registShareSDKInAppDelegate{
                [ShareSDK registerActivePlatforms:@[
                                                    @(SSDKPlatformTypeSinaWeibo),
                                                    @(SSDKPlatformTypeCopy),
                                                    @(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:@"3839644528"
                                                       appSecret:@"df0eb6e3707245b7f5becf46c0312004"
                                                     redirectUri:@"http://itunes.apple.com/cn/app/id1313283200?mt=8"
                                                        authType:SSDKAuthTypeBoth];
                             break;
                         case SSDKPlatformTypeWechat:
                             [appInfo SSDKSetupWeChatByAppId:@"wx6974c32f2be3a8fe"
                                                   appSecret:@"8639d7d8139bc8c07ebcfa8751013a05"];
                             break;
                         case SSDKPlatformTypeQQ:
                             [appInfo SSDKSetupQQByAppId:@"1106375457"
                                                  appKey:@"Jo4WrLiKIs9GXbsj"
                                                authType:SSDKAuthTypeBoth];
                             break;
                         default:
                             break;
                     }
                 }];
            }
    
            2.在自定义分享菜单地方,调用方法并传值
    
                - (void)shareDetailBtnClick:(UIButton *)sender{
                    MSUDanamicVideoModel *videoModel = _dataModel.d_states[self.modelIndex];
                
                    NSInteger code = 0;
                    if (sender == _shareView.btnArr[0]) {
                        code = 0;
                    } else if (sender == _shareView.btnArr[1]){
                        code = 1;
                    } else if (sender == _shareView.btnArr[2]){
                        code = 2;
                    }else{
                        code = 3;
                    }
                    [MSUShareController showPlatformWithNoUIWithCode:code title:@"秀百贝" content:videoModel.video_brife imageStr:videoModel.video_img];
                }
    
            3. MSUShareController 类中相关代码
    
                1) MSUShareController.h
    
                    #import <UIKit/UIKit.h>
    
                    @interface MSUShareController : UIViewController
                    
                    /* ShareSDK 分享内容相关 */
                    + (void)makeShareSomeContentWithText:(NSString *)text title:(NSString *)title url:(NSString *)url;
                    
                    /* ShareSDK 分享内容相关 */
                    + (void)shareSomething;
                    
                    + (void)showPlatformWithNoUIWithCode:(NSInteger)code title:(NSString *)title content:(NSString *)content imageStr:(NSString *)imageStr;
                    
                    @end
    
                2) MSUShareController.m
    
                    #import "MSUShareController.h"
    
                    // ShareSDK
                    #import <ShareSDK/ShareSDK.h>
                    #import <ShareSDKUI/ShareSDK+SSUI.h>
                    
                    #import "MSUPathTools.h"
                    
                    @interface MSUShareController ()
                    
                    @end
                    
                    @implementation MSUShareController
                    
                    /* ShareSDK 分享内容相关 */
                    + (void)makeShareSomeContentWithText:(NSString *)text title:(NSString *)title url:(NSString *)url{
                    
                        //1、创建分享参数
                        NSArray* imageArray = @[[UIImage imageNamed:@"logo"]];
                        //(注意:图片必须要在Xcode左边目录里面,名称必须要传正确,如果要分享网络图片,可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])
                        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:@[@(SSDKPlatformSubTypeWechatSession),
                                                             @(SSDKPlatformSubTypeWechatTimeline),
                                                             @(SSDKPlatformTypeQQ),
                                                             @(SSDKPlatformTypeSinaWeibo)]
                                               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;
                                           }
                                       }
                             ];
                        }
                    }
                    
                    
                    // 跳过编辑页面 带系统UI
                    + (void)shareSomething{
                        //先构造分享参数:
                        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
                        [shareParams SSDKSetupShareParamsByText:@"分享内容"
                                                         images:@[[UIImage imageNamed:@"logo"]]
                                                            url:[NSURL URLWithString:@"http://mob.com"]
                                                          title:@"分享标题"
                                                           type:SSDKContentTypeAuto];
                        //有的平台要客户端分享需要加此方法,例如微博
                        [shareParams SSDKEnableUseClientShare];
                        //调用分享的方法
                        SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:nil
                                                                                         items:nil
                                                                                   shareParams:shareParams
                                                                           onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                                                                               switch (state) {
                                                                                   case SSDKResponseStateSuccess:
                                                                                       NSLog(@"分享成功!");
                                                                                       break;
                                                                                   case SSDKResponseStateFail:
                                                                                       NSLog(@"分享失败%@",error);
                                                                                       break;
                                                                                   case SSDKResponseStateCancel:
                                                                                       NSLog(@"分享已取消");
                                                                                       break;
                                                                                   default:
                                                                                       break;
                                                                               }
                                                                           }];
                        //删除和添加平台示例
                        //    [sheet.directSharePlatforms removeObject:@(SSDKPlatformTypeWechat)];//(默认微信,QQ,QQ空间都是直接跳客户端分享,加了这个方法之后,可以跳分享编辑界面分享)
                        [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];//(加了这个方法之后可以不跳分享编辑界面,直接点击分享菜单里的选项,直接分享)
                    }
                    
                    /*  跳过编辑页面 无UI */
                    + (void)showPlatformWithNoUIWithCode:(NSInteger)code title:(NSString *)title content:(NSString *)content imageStr:(NSString *)imageStr{//SSDKPlatformTypeQQ,SSDKPlatformTypeWechat,SSDKPlatformSubTypeWechatTimeline,SSDKPlatformTypeSinaWeibo
                       
                        if (((code == 0 || code == 1) && ![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) || (code == 2 && ![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"sinaweibo://"]]) || (code == 3 && ![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]])) {
                            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"您当前手机尚未安装相关APP应用" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                            [alert show];
                        } else{
                            SSDKPlatformType type;
                            if (code == 0) {
                                type = SSDKPlatformTypeWechat;
                            } else if (code == 1){
                                type = SSDKPlatformSubTypeWechatTimeline;
                            } else if (code == 2){
                                type = SSDKPlatformTypeSinaWeibo;
                            } else{
                                type = SSDKPlatformSubTypeQZone;
                            }
                            
                            UIImage *image;
                            if (imageStr.length == 0) {
                                image = [MSUPathTools showImageWithContentOfFileByName:@"search-headbig"];
                            } else{
                                image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://psource.showbuy100.com%@",imageStr]]]];
                            }
                            //创建分享参数
                            NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
                            [shareParams SSDKSetupShareParamsByText:content
                                                             images:image //传入要分享的图片
                                                                url:[NSURL URLWithString:@"http://itunes.apple.com/cn/app/id1313283200?mt=8"]
                                                              title:title
                                                               type:SSDKContentTypeAuto];
                            
                            //进行分享
                            [ShareSDK share:type //传入分享的平台类型
                                 parameters:shareParams
                             onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { // 回调处理....}];
                                 switch (state) {
                                     case SSDKResponseStateSuccess:
                                         NSLog(@"分享成功!");
                                         break;
                                     case SSDKResponseStateFail:
                                         NSLog(@"分享失败%@",error);
                                         if (error.code == 208) {
                                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"您当前手机尚未安装相关APP应用" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                                             [alert show];
                                             
                                         }
                                         
                                         break;
                                     case SSDKResponseStateCancel:
                                         NSLog(@"分享已取消");
                                         break;
                                     default:
                                         break;
                                 }
                             }];
                        }
                    }
    

    ** 五.三方登录**

        - (void)qqBtnClick:(UIButton *)sender{
    //    [MSUHUD showFileWithString:@"此功能暂未开放"];
        [ShareSDK getUserInfo:SSDKPlatformTypeQQ // 如果登录什么平台,此处更换即可SSDKPlatformTypeWechat,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);
                 [self loadRequestWithDataId:user.uid type:@"qq"];
             }
             
             else
             {
                 NSLog(@"%@",error);
             }
             
         }];
    }

    相关文章

      网友评论

          本文标题: iOS - 第三方分享和登陆 SSO(免登录)

          本文链接:https://www.haomeiwen.com/subject/huutyttx.html