iOS移动端第三方登录集成实现

NSArray *imageArray=[[NSArray alloc]initWithObjects:@"btn_3_weixin",@"btn_3_qq",@"btn_3_weibo", nil];
                    UIImageView  *shareButton;
                    CGFloat firstButtonWdith=0;
                    CGFloat  axisY=functionButton.frame.origin.y+functionButton.frame.size.height+40;
                    CGFloat thisWidht=DEVICE_Width-76;
                    
                    UILabel *thisShareLabel;
                    for (int sh=0;sh<3;sh++) {
                        shareButton=[[UIImageView alloc]init];
                        if (sh==0) {
                            thisShareLabel=[[UILabel alloc]initWithFrame:CGRectMake(38, axisY+7, thisWidht*0.25, 1)];
                            thisShareLabel.backgroundColor=[UIColor whiteColor];
                            shareButton.frame = CGRectMake(  thisShareLabel.frame.origin.x+thisShareLabel.frame.size.width*0.18 , thisShareLabel.frame.origin.y+20,thisShareLabel.frame.size.width*0.64 , thisShareLabel.frame.size.width*0.64);
                            
                            firstButtonWdith=shareButton.frame.size.width;
                            
                        }
                        else if(sh==1)
                        {
                            thisShareLabel=[[UILabel alloc]initWithFrame:CGRectMake(thisShareLabel.frame.size.width+thisShareLabel.frame.origin.x, axisY, thisWidht*0.5, 14)];
                            thisShareLabel.text=@"第三方账户登录";
                            
                            shareButton.frame = CGRectMake(  thisShareLabel.frame.origin.x+thisShareLabel.frame.size.width/2-(firstButtonWdith/2) , thisShareLabel.frame.origin.y+26,firstButtonWdith, firstButtonWdith);
                           
                        }
                        else
                        {
                            thisShareLabel=[[UILabel alloc]initWithFrame:CGRectMake(thisShareLabel.frame.size.width+thisShareLabel.frame.origin.x, axisY+7, thisWidht*0.25, 1)];
                            thisShareLabel.backgroundColor=[UIColor whiteColor];
                            
                            shareButton.frame = CGRectMake(  thisShareLabel.frame.origin.x+thisShareLabel.frame.size.width*0.18 , thisShareLabel.frame.origin.y+20,thisShareLabel.frame.size.width*0.64 , thisShareLabel.frame.size.width*0.64);
                        }
                        thisShareLabel.font=[UIFont systemFontOfSize:14];
                        thisShareLabel.textAlignment=NSTextAlignmentCenter;
                        thisShareLabel.textColor=[UIColor whiteColor];
                        [self addSubview:thisShareLabel];
                      
                        [shareButton setImage:[UIImage imageNamed:[imageArray objectAtIndex:sh]]];
                        shareButton.userInteractionEnabled=YES;
                        UIGestureRecognizer *thirdSDKLoginUIGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(thirdSDKLogin:)];
                        [shareButton addGestureRecognizer:thirdSDKLoginUIGestureRecognizer];
                         shareButton.tag=123456+sh;
                        [self addSubview:shareButton];
                        if (sh==2) {
                            self.backScrollView .scrollEnabled=YES;
                            if (DEVICE_Height<500) {
                                [self.backScrollView  setContentSize:CGSizeMake(DEVICE_Width, self.backScrollView.contentSize.height+50)];
                            }
                        }
                      
                        
                    }


这里首先需要实现功能的ViewController中引入对应的库

#import "UMSocial.h"

#import "WXApi.h" 

#import <TencentOpenApi/QQApiInterface.h>

#import "WeiboSDK.h"

下面的方法就是就是上面代码中UIImageView  *shareButton绑定的手势,这里为什么要使用UIImageView,

因为我代码布局中 shareButton的宽度和高度没有固定,是根据屏幕的宽度来计算的,如果使用UIButton就会出现

贴的图片的大小不会随着动态计算出的高宽而缩放,如果写代码放大缩小又会出现模糊状态。


//公用的跳转用户资料详情页面
-(void)thirdSDKLogin:(UITapGestureRecognizer *)sender

{
    UIImageView *view=(UIImageView *)sender.self.view;
    if (view.tag==123456 ) {
        //判断微信客户端是否安装
        if ([WXApi isWXAppInstalled]) {
            NSString *platformName = [UMSocialSnsPlatformManager getSnsPlatformString:UMSocialSnsTypeWechatSession];
            
            UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
            
            snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
                
                NSLog(@"login response is %@",response);
                
                //获取微博用户名、uid、token等
                
                if (response.responseCode == UMSResponseCodeSuccess) {
                    
                    UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:platformName];
                    
                    NSLog(@"username is %@, uid is %@, token is %@,iconUrl is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
                    
                }
                
            });
        }
        else
        {
            showMessage(@"请先安装微信客户端。");
            return;
        }

    }
     else if(view.tag==123457)
     {
         //判断qq客户端是否安装
         if ([QQApiInterface isQQInstalled]) {
             NSString *platformName = [UMSocialSnsPlatformManager getSnsPlatformString:UMSocialSnsTypeMobileQQ];
             
             UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
             
             snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
                 
                 NSLog(@"login response is %@",response);
                 
                 //获取微博用户名、uid、token等
                 
                 if (response.responseCode == UMSResponseCodeSuccess) {
                     
                     UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:platformName];
                   NSLog(@"username is %@, uid is %@, token is %@,iconUrl is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
                     
                 }
                 
             });
         }
         else{
             showMessage(@"请先安装qq客户端。");
             return;
         }
     }
     else
     {
         
         //判断sina客户端是否安装
         if ([WeiboSDK isCanShareInWeiboAPP])
             
         {
             /*新浪登录第三方登录授权*/
             NSString *platformName = [UMSocialSnsPlatformManager getSnsPlatformString:UMSocialSnsTypeSina];
             UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
             
             snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
                 
                 NSLog(@"response is %@",response);
                 
                 if (response.responseCode == UMSResponseCodeSuccess) {
                     
                     UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:platformName];
                     
                     NSLog(@"=========%@",snsAccount.accessToken);
                     
                 }
                 
             });

             
         }
         
         else
             
         {
             
             showMessage(@"请先安装新浪微博客户端。");
             return;
             
             
         }
     }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值