美文网首页
ShareSDK微信、QQ登录,直接代码可用

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

作者: 远航Yang | 来源:发表于2017-10-25 17:37 被阅读0次

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

    }

    }

    }];

    }

    相关文章

      网友评论

          本文标题:ShareSDK微信、QQ登录,直接代码可用

          本文链接:https://www.haomeiwen.com/subject/uaxcpxtx.html