IOS 整合第三方登入,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 }