你的位置:首页 > 操作系统

[操作系统]IOS 集成第三方登录


     我使用的是友盟上集成的第三方登录功能,一共使用了三个应用的登录授权,QQ、微信、新浪微博。由于第三方登录授权成功后,需要跳转到一个新的界面,所以这里需要在项目里设置第三方登录的SSO授权。就是必须安装了相关的手机客户端后,才能使用第三方登录,在使用第三方登录时,我们需要先判断一下用户手机上是否已经安装了对应的应用。

一、集成SSO授权

    这里集成SSO授权的方法我就不详细讲解了,因为还涉及到注册第三方平台帐号这些琐屑的事。下面我给一个友盟上集成SSO授权的地址,文档说明都是很详细的

地址:http://dev.umeng.com/social/ios/detail-share#6  如果你中途遇到任何问题,都可以在这里留言,接下去我主要是贴代码和实现效果截图。

二、对应控件的布局代码

 1  NSArray *imageArray=[[NSArray alloc]initWithObjects:@"btn_3_weixin",@"btn_3_qq",@"btn_3_weibo", nil]; 2           UIImageView *shareButton; 3           CGFloat firstButtonWdith=0; 4           CGFloat axisY=functionButton.frame.origin.y+functionButton.frame.size.height+40; 5           CGFloat thisWidht=DEVICE_Width-76; 6            7           UILabel *thisShareLabel; 8           for (int sh=0;sh<3;sh++) { 9             shareButton=[[UIImageView alloc]init];10             if (sh==0) {11               thisShareLabel=[[UILabel alloc]initWithFrame:CGRectMake(38, axisY+7, thisWidht*0.25, 1)];12               thisShareLabel.backgroundColor=[UIColor whiteColor];13               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);14               15               firstButtonWdith=shareButton.frame.size.width;16               17             }18             else if(sh==1)19             {20               thisShareLabel=[[UILabel alloc]initWithFrame:CGRectMake(thisShareLabel.frame.size.width+thisShareLabel.frame.origin.x, axisY, thisWidht*0.5, 14)];21               thisShareLabel.text=@"第三方账户登录";22               23               shareButton.frame = CGRectMake( thisShareLabel.frame.origin.x+thisShareLabel.frame.size.width/2-(firstButtonWdith/2) , thisShareLabel.frame.origin.y+26,firstButtonWdith, firstButtonWdith);24              25             }26             else27             {28               thisShareLabel=[[UILabel alloc]initWithFrame:CGRectMake(thisShareLabel.frame.size.width+thisShareLabel.frame.origin.x, axisY+7, thisWidht*0.25, 1)];29               thisShareLabel.backgroundColor=[UIColor whiteColor];30               31               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);32             }33             thisShareLabel.font=[UIFont systemFontOfSize:14];34             thisShareLabel.textAlignment=NSTextAlignmentCenter;35             thisShareLabel.textColor=[UIColor whiteColor];36             [self addSubview:thisShareLabel];37            38             [shareButton setImage:[UIImage imageNamed:[imageArray objectAtIndex:sh]]];39             shareButton.userInteractionEnabled=YES;40             UIGestureRecognizer *thirdSDKLoginUIGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(thirdSDKLogin:)];41             [shareButton addGestureRecognizer:thirdSDKLoginUIGestureRecognizer];42             shareButton.tag=123456+sh;43             [self addSubview:shareButton];44             if (sh==2) {45               self.backScrollView .scrollEnabled=YES;46               if (DEVICE_Height<500) {47                 [self.backScrollView setContentSize:CGSizeMake(DEVICE_Width, self.backScrollView.contentSize.height+50)];48               }49             }50            51             52           }

 

 

上面当然只是登录界面中第三方登录块的部分代码,是相对于上一个控件 functionButton来布局的,下面是布局效果截图

三、第三方登录实现代码

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

#import "UMSocial.h"

#import "WXApi.h" 

#import <TencentOpenApi/QQApiInterface.h> 

#import "WeiboSDK.h"

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

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

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

 1 //公用的跳转用户资料详情页面 2 -(void)thirdSDKLogin:(UITapGestureRecognizer *)sender 3  4 { 5   UIImageView *view=(UIImageView *)sender.self.view; 6   if (view.tag==123456 ) { 7     //判断微信客户端是否安装 8     if ([WXApi isWXAppInstalled]) { 9       NSString *platformName = [UMSocialSnsPlatformManager getSnsPlatformString:UMSocialSnsTypeWechatSession]; 10        11       UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession]; 12        13       snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){ 14          15         NSLog(@"login response is %@",response); 16          17         //获取微博用户名、uid、token等 18          19         if (response.responseCode == UMSResponseCodeSuccess) { 20            21           UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:platformName]; 22            23           NSLog(@"username is %@, uid is %@, token is %@,iconUrl is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL); 24            25         } 26          27       }); 28     } 29     else 30     { 31       showMessage(@"请先安装微信客户端。"); 32       return; 33     } 34  35   } 36   else if(view.tag==123457) 37    { 38     //判断qq客户端是否安装 39     if ([QQApiInterface isQQInstalled]) { 40       NSString *platformName = [UMSocialSnsPlatformManager getSnsPlatformString:UMSocialSnsTypeMobileQQ]; 41        42       UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ]; 43        44       snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){ 45          46         NSLog(@"login response is %@",response); 47          48         //获取微博用户名、uid、token等 49          50         if (response.responseCode == UMSResponseCodeSuccess) { 51            52           UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:platformName]; 53          NSLog(@"username is %@, uid is %@, token is %@,iconUrl is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL); 54            55          } 56          57        }); 58      } 59     else{ 60       showMessage(@"请先安装qq客户端。"); 61       return; 62      } 63    } 64   else 65    { 66      67     //判断sina客户端是否安装 68     if ([WeiboSDK isCanShareInWeiboAPP]) 69        70      { 71       /*新浪登录第三方登录授权*/ 72       NSString *platformName = [UMSocialSnsPlatformManager getSnsPlatformString:UMSocialSnsTypeSina]; 73       UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina]; 74        75       snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){ 76          77         NSLog(@"response is %@",response); 78          79         if (response.responseCode == UMSResponseCodeSuccess) { 80            81           UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:platformName]; 82            83           NSLog(@"=========%@",snsAccount.accessToken); 84            85          } 86          87        }); 88  89        90      } 91      92     else 93        94      { 95        96       showMessage(@"请先安装新浪微博客户端。"); 97       return; 98        99       100      }101    }102 }