ShareSDK微信、QQ登录,直接代码可用

2017-10-25  本文已影响0人  远航Yang

ShareSDK都封装好第三方登录的方法了,非常方便调用,直接拷贝下面的方法,即可实现第三方登录。

1、微信登录方法

- (void)clickedWxLogin {

if ([ShareSDK hasAuthorized:SSDKPlatformTypeWechat]) {//判断是否授权

      [ShareSDK cancelAuthorize:SSDKPlatformTypeWechat];//取消授权

}

WEAKSELF

[ShareSDK getUserInfo:SSDKPlatformTypeWechatonStateChanged:^(SSDKResponseStatestate,SSDKUser*user,NSError*error) {

weakSelf.wxButton.enabled=YES;

if(state ==SSDKResponseStateSuccess&& user) {

NSLog(@"wx data : \n%@",user.rawData);

NSString*unionid = [user.rawDatatheValueForKey:@"unionid"];

if(!unionid || unionid.length==0) {

unionid = user.uid;

}

[ShareSDK setCurrentUser:userforPlatformType:SSDKPlatformTypeWechat];

}

else{

NSLog(@"error : %@",error);

if([ShareSDKhasAuthorized:SSDKPlatformTypeWechat]) {

[ShareSDKcancelAuthorize:SSDKPlatformTypeWechat];

}

if(state ==SSDKResponseStateFail) {

[weakSelfshowHUDError:@"登录超时,请重新登录"];

}

}

}];

}

2、QQ登录方法

-(IBAction)qqLoginAction:(UIButton*)button {

if([ShareSDKhasAuthorized:SSDKPlatformTypeQQ]) {

[ShareSDKcancelAuthorize:SSDKPlatformTypeQQ];

}

WEAKSELF

[ShareSDK getUserInfo:SSDKPlatformTypeQQonStateChanged:^(SSDKResponseStatestate,SSDKUser*user,NSError*error) {

if(state ==SSDKResponseStateSuccess&& user) {

NSLog(@"qq data : \n%@",user.rawData);

[ShareSDK setCurrentUser:userforPlatformType:SSDKPlatformTypeQQ];

}];

}

else{

NSLog(@"error : %@",error);

if([ShareSDK hasAuthorized:SSDKPlatformTypeQQ]) {

[ShareSDK cancelAuthorize:SSDKPlatformTypeQQ];

}

if(state ==SSDKResponseStateFail) {

[weakSelf showHUDError:@"登录超时,请重新登录"];

}

}

}];

}

上一篇下一篇

猜你喜欢

热点阅读