facebook+cocos2d-x (三)排行榜、获取用户头像

获取用户头像并不需要额外的权限,但是排行榜需要用到publish_actions权限,所以在使用之前需要先获取该权限。并且这个权限不能在登录的时候跟那些只读权限一起获取,需要单独获取。

1、判断当前是否具有publish_actions权限

 if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"])

 {

      return true;

 }else

 {

      return false;

 }

2、申请publish_actions权限

AppController * pController=[AppController getSharedInstance];

FBSDKLoginManager * manager=[[FBSDKLoginManager allocinit];

NSArray *permissions = [[NSArray alloc] initWithObjects: @"publish_actions", nil];

[manager logInWithPublishPermissions:permissions fromViewController:pController.viewController handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)

        {

           

         if (error)

         {

             //do something;

         }else 

         {

             //do something;

         }

        }];

3、上传用户分数

 

    NSString *score = [NSString stringWithFormat:@"%d", 12345];//12345替换成要上传的分数

    NSDictionary *params = @{@"score": score};

    

    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]

                                  initWithGraphPath:@"/me/scores"

                                  parameters:params

                                  HTTPMethod:@"POST"];

    

    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

        // Handle the result

        if(!error && result)

        {

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

            return;

        }else{

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

        }

        

    }];

4、获取用户分数

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]

                                  initWithGraphPath:[NSString stringWithFormat:@"%@/scores", [FBSDKSettings appID]]

                                  parameters:@{@"fields":@"score,user"}

                                  HTTPMethod:@"GET"];

    

    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

        // Handle the result

        if(!error && result){

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

            

            NSArray* fetchedScoreData = [[NSArray alloc] initWithArray:[result objectForKey:@"data"]];

            if ([fetchedScoreData count] > 0){

               // do something

               

            }

        }else{

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

        }

        

    }];

5、获取用户头像

这里将获取的头像保存到文件里面,需要注意的一点是writeToFile操作完成的时候,如果立即用该图像生成一个Sprite会出现头像是一个黑色方块的情况。可以通过在一小段延迟之后,再使用该图像来避免黑块的情况。

   

    string strId="xxxxxxxxxxx";//用户id

     int width=200//实际宽高可以根据需求修改

    int height=200;

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%s/picture?width=%d&height=%d", strId.c_str(), width ,height]];

    dispatch_queue_t downloader = dispatch_queue_create("PicDownloader"NULL);

    dispatch_async(downloader, ^

                   {

                       

                      string strSavePath=FileUtils::getInstance()->getWritablePath();

                       strSavePath+="FBImage";

                        strSavePath+=strId+".png";

                       NSData *data = [NSData dataWithContentsOfURL:url];

                       UIImage *image = [UIImage imageWithData:data];

                       NSString * sPic = [NSString stringWithFormat:@"%s",strSavePath.c_str()];

                       if([UIImagePNGRepresentation(image) writeToFile:sPic atomically:YES])

                       {

                            //do something;

                       }

                       

                      

                       

                   });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五仁烧饼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值