iOS Developer

Facebook索取物品功能梳理

2016-07-01  本文已影响127人  Joshua520

关于博客,一直觉得自己文笔不好,不适合写一些东西。最近面试总会有人问你有没有自己的技术博客抑或开发过程中遇到的问题为什么不整理出来。今天试着写一下Facebook关于索取这块功能的梳理。下面进入正文:

Facebook官方接入文档:https://developers.facebook.com/docs/games/requests/v2.3

1,关于索取的流程描述:索取界面(可以是按钮或者FB好友列表界面) -> 索取请求

获取请求-> 实现请求界面-> 处理请求信息(接受/取消) -> 删除请求

2.索取/发送请求实现:

#pragma mark 索取/赠送物品接口 function

- (void)FbGameRequestType:(FBSDKGameRequestActionType)actionType title:(NSString *)title objectID:(NSString *)objectID mesage:(NSString *)message to:(NSArray *)friendsID extraData:(NSString *)extraData handle:(R2SDKHandler)handle{

_handle = [handle copy];

FBSDKGameRequestContent *content = [[FBSDKGameRequestContent alloc] init];

content.actionType = actionType;

content.message = message;

content.title = title;

content.objectID = objectID;

if (friendsID) {

content.to = friendsID;

}

content.data = extraData;

FBSDKGameRequestDialog *dialog = [[FBSDKGameRequestDialog alloc] init];

dialog.content = content;

[content release];

dialog.delegate = self;

[dialog show];

[dialog release];

}

参数说明:

actionType:请求类型(发送/索取)对应FB的FBSDKGameRequestActionTypeSend、FBSDKGameRequestActionTypeAskFor

message:请求内容

title:请求标题

objectID:请求物品的objectID

friendsID:好友的FacebookID(如果为空,会出现选择好友界面)

extraData:自定义内容,可用于区分请求类型

返回说明:

results : {

request =101166043550679;

"to[0]" = 1556010831327419;

}

3、获取请求

#pragma mark 获取请求信息 function

- (NSArray *)notificationGet{

if ([FBSDKAccessToken currentAccessToken]) {

NSString *urlStr = [NSString stringWithFormat:@"https://graph.facebook.com/me/apprequests?access_token=%@",[FBSDKAccessToken currentAccessToken].tokenString];

NSURL *url  =[NSURL URLWithString:urlStr];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0];

[request setHTTPMethod:@"GET"];

NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

if (receiveData) {

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:receiveData options:NSJSONReadingMutableLeaves error:nil];

NSArray *requestArray = [jsonDic objectForKey:@"data"];

return requestArray;

}else{

return nil;

}

}else{

return nil;

}

}

返回说明:

{

application=        {

id =816966971726400;

name =R2FunGames;

namespace = "lx_rtwogames";

};

"created_time" = "2015-04-01T09:05:14+0000";

from =        {

id =365056257014686;

name ="David Lu";

};

id ="1392376167750176_360466704143920";

message ="David is testing";

to =        {

id =360466704143920;

name ="Hohua  Huang";

};

},...

}

注释:获取请求必须在FB登陆成功的状态下

4、删除请求

在接受请求后,调用发送物品请求接口,发送物品。处理成功后需要删除该请求。(不接受直接删除)

参见如下代码:()

#pragma mark 删除处理完的请求 function

- (NSDictionary *)notificationClear:(NSString *)requestid{

if ([FBSDKAccessToken currentAccessToken]) {

NSString *urlStr = [NSString stringWithFormat:@"https://graph.facebook.com/%@?access_token=%@",requestid,[FBSDKAccessToken currentAccessToken].tokenString];

NSURL *url =[NSURL URLWithString:urlStr];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"DELETE"];

NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:receiveData options:NSJSONReadingMutableLeaves error:nil];

return json;

}

return nil;

}

5、索取物品的配置

参见:https://developers.facebook.com/docs/games/requests/v2.3

上一篇下一篇

猜你喜欢

热点阅读