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

96
远航Yang
2017.10.25 17:37* 字数 238

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:@"登录超时,请重新登录"];

}

}

}];

}

日记本
Web note ad 1