我编写了以下代码,它将预定义的GraphAPI对象发送到我的iOS应用中的另一个Facebook用户。这很好用。
-(void)FacebookSendObjectWithIdentifier: (NSString *)objectIdentifier withObjectName:(NSString *)objectName withMessage:(NSString *)message withTitle:(NSString *)title withActionType:(NSString *)actionType runOnComplete:(void (^)(NSDictionary* request))completionBlock{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
// Optional parameter for sending request directly to user
// with UID. If not specified, the MFS will be invoked
//@"RECIPIENT_USER_ID", @"to",
// Give the action object request information
actionType, @"action_type",
objectIdentifier, @"object_id",
nil];
[FBWebDialogs
presentRequestsDialogModallyWithSession:nil
message:message
title:title
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// Error launching the dialog or sending the request.
NSLog(@"Error sending %@ request.", objectName);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User clicked the "x" icon
NSLog(@"User canceled %@ request.", objectName);
} else {
// Handle the send request callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:@"request"]) {
// User clicked the Cancel button
NSLog(@"User canceled %@ request.", objectName);
} else {
// User clicked the Send button
NSString *requestID = [urlParams valueForKey:@"request"];
NSLog(@"%@ request sent, request id: %@", objectName, requestID);
if (completionBlock)
completionBlock(urlParams);
}
}
}
}];
}
[self parseURLParams:[resultURL query]];在几个Facebook文档页面中实现如下:
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:@"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:@"="];
NSString *val =
[kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
params[kv[0]] = val;
}
return params;
}
返回时,完成块解析返回的urlParams并将它们保存到我们的数据库中以引用通过Facebook发送的对象。但是,这应该返回一个"请求"保存请求ID的参数和数组"到"它包含发送对象的人员的Facebook ID。相反,这会返回网址中的两个字段," request"行为正常,"到[0]"而不是"到#34;作为NSString而不是NSArray,因为上面提供的方法将url参数解析为字符串而不是相应的对象,这将数组"转换为"进入"到[0]"的NSString。
我正在寻找一个更好的解决方案来解析来自Facebook的返回响应URL或更好的方式通过Facebook的API发送对象。最好是第一个选项,因为这是实现完成所缺少的唯一步骤。
谢谢。