Facebook SDK 3.0 for iOS: Can't set omit_response_on_success for batch requests
-
25-06-2021 - |
题
It seems for batch requests, all the parameters are escaped as parts of relative_url, if omit_response_on_success is set to @(false), app will crash with this message: -[__NSCFNumber length]: unrecognized selector
NSDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys: @(false), @"omit_response_on_success", nil];
FBRequest *request1 = [FBRequest requestWithGraphPath:self.graphPath
parameters:parameters
HTTPMethod:nil];
[newConnection addRequest:request1 completionHandler:handler batchEntryName:@"entryName"];
If the graphPath is set to @"me/home?omit_response_on_success=0"
, these will be no output from this operation. Any ideas?
解决方案
Yes, this option is currently not supported by the SDK as-is, be sure to file a feature request on https://developers.facebook.com/bugs for this.
其他提示
That should not be a parameter but a key-value in the JSON body of the request, as noted in the docs. I believe the question is rather how to set that key-value in the iOS SDK since we don't have access to the body of the request. From what I could tell there is no way to do it, but I'm not sure if it's a bug.
It's very annoying that Facebook doesn't allow us to set this flag using the iOS SDK. I spent hours trying to figure out a way and this is a little hack I came up with. It should be relatively safe. Just use the RSFBRequestConnection instead of FBRequestConnection:
@interface RSFBRequestConnection : FBRequestConnection
@end
@implementation RSFBRequestConnection
- (NSMutableURLRequest *)urlRequest
{
NSMutableURLRequest *request = [super urlRequest];
NSData *body = request.HTTPBody;
NSString *bodyStr = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(@"%@", bodyStr);
NSString *fixed = [bodyStr stringByReplacingOccurrencesOfString:@"\"relative_url\"" withString:@"\"omit_response_on_success\":false,\"relative_url\""];
request.HTTPBody = [fixed dataUsingEncoding:NSUTF8StringEncoding];
return request;
}
@end