ShareKit/ShareKit — Issue #184: SHKFacebook error saving kSHKFacebookUserInfo to NSUserDefaults
Author: brendand
Created: 2012-01-19 07:03:49+00:00
State: open

In the response dictionary that comes back from Facebook in the request:didLoad: call, one of the values in the dictionary is a CFNull. This cannot be stored in the NSUserDefaults, so consequently, you can't query the NSUserDefaults later on to get out any values.

So I added this code to get around the problem:

- (void)cleanDictionary:(NSMutableDictionary *)dictionary {
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (obj == [NSNull null]) {
[dictionary setObject:@"" forKey:key];
} else if ([obj isKindOfClass:[NSDictionary class]]) {
[self cleanDictionary:obj];
}
}];
}

- (void)request:(FBRequest *)request didLoad:(id)result {

[self cleanDictionary:result];

if ([result objectForKey:@"username"]) {
[[NSUserDefaults standardUserDefaults] setObject:result forKey:kSHKFacebookUserInfo];
}

[self sendDidFinish];
}

It recursively traverses the NSDictionary looking for Nulls to convert to the empty string.

Not sure if this is the best way to handle it, but it works for me. It'd be nice if this fix was put into the base ShareKit 2.0.

Thanks!

Brendan