Skip to content

Instantly share code, notes, and snippets.

@convexstyle
Last active December 20, 2015 20:29
Show Gist options
  • Save convexstyle/6190958 to your computer and use it in GitHub Desktop.
Save convexstyle/6190958 to your computer and use it in GitHub Desktop.
Upload Photo to Facebook Timeline with privacy setting using Facebook SDK.
/*
References :
https://developers.facebook.com/docs/reference/api/photo/
https://developers.facebook.com/docs/reference/api/privacy-parameter/
*/
// Upload Image
UIImage *uploadImage = /* assign your UIImage */
//--- Post Parameters ---//
NSMutableDictionary *params = [NSMutableDictionary dictionary];
// Access token (Usually, it should be retrieved from oAuth.)
[params setObject:[FBSession activeSession].accessTokenData.accessToken forKey:@"access_token"];
// Photo Caption
[params setObject:aCaptionText forKey:@"name"];
// Privacy Setting
NSDictionary *privacy = [NSDictionary dictionaryWithObjectsAndKeys:@"SELF", @"value", nil];
NSError *jsonError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:privacy options:0 error:&jsonError];
if(!jsonError) {
NSString *privacyStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[params setObject:privacyStr forKey:@"privacy"];
}
// Upload Image (It doesn't need to be binary data.)
[params setObject:uploadImage forKey:@"picture"];
//--- Upload Image ---//
[FBRequestConnection startWithGraphPath:@"me/photos/" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(error) {
dispatch_async(dispatch_get_main_queue(), ^{
// Failed. Show Error Message
});
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
// Success ! Update UI
});
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment