Skip to content

Instantly share code, notes, and snippets.

@ekoneil
Last active December 24, 2015 14:39
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekoneil/6813974 to your computer and use it in GitHub Desktop.
Save ekoneil/6813974 to your computer and use it in GitHub Desktop.
[Facebook iOS SDK example] Publish an Open Graph (OG) story that: 1/ uses image staging 2/ creates a custom, user-owned OG object 3/ creates a custom OG action
UIImage* image = [UIImage imageNamed:@"inspiration-2.jpg"]; // image stored in app's resources
// stage an image
[FBRequestConnection startForUploadStagingResourceWithImage:image
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(error) {
NSLog(@"Error staging resource.\n%@", error);
int code = [[[[[error userInfo] objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"] objectForKey:@"code"];
if(code == 2500) {
NSLog(@"...couldn't find an access token. Gotta log in to post stories via the API.");
}
} else {
NSLog(@"Successfuly staged image with staged URI: %@", [result objectForKey:@"uri"]);
NSString* stagedImageId = [result objectForKey:@"uri"];
NSLog(@"Staged Image URI: %@", stagedImageId);
// configure custom, user-owned object. default audience == $app-ceiling.
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
object.provisionedForPost = YES;
object[@"title"] = @"Inspiration";
object[@"type"] = @"fbsdktoolkit:route";
object[@"description"] = @"Mazama Inspiration route on Goat Wall";
object[@"image"] = @[
@{@"url": stagedImageId, @"user_generated" : @"true" }
];
// post the object
[FBRequestConnection startForPostOpenGraphObject:object
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success. %@", result);
}
NSString* objectId = [result objectForKey:@"id"]; // read object ID from previous response
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:@"route"];
// create action referencing user owned object. default audience == $app-ceiling
[FBRequestConnection startForPostWithGraphPath:@"/me/fbsdktoolkit:climb"
graphObject:action
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success. Created action with ID: %@", [result objectForKey:@"id"]);
NSLog(@"See the story at: https://www.facebook.com/{your-vanity-url}/activity/%@", [result objectForKey:@"id"]);
}
}];
}];
}
}];
@balazsnemeth
Copy link

It is posting, and using the post URL (logged in case of success) I can see the post, but it is not on my FB on the web.

@steventsng
Copy link

@balazsnemeth Not sure if you figured it out. But from this stackoverflow answer, you can do this after line 20:

[object setObject: @"true" forKey: @"fb:explicitly_shared"];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment