Publishing Facebook custom story using API calls from IOS app

1.1k views Asked by At

I need to publish Open Graph story to Facebook wall from my app. I followed current tutorial on Facebook developers.

I've created object,action and story on Facebook dashboard. Then in my app do the following code:

create an object:

 NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost]; 
object.provisionedForPost = YES;
object[@"title"] = @"test title";
object[@"type"] = @"myapp:my_object_type";
object[@"description"] = @"test discription";
object[@"url"] = @"http://example.com";
object[@"image"] = @[@{@"url": [result objectForKey:@"uri"], @"user_generated" : @"false" }];

and post it:

[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if(!error) {
                    NSString *objectId = [result objectForKey:@"id"];
                    NSLog(@"object id %@",objectId);
                } else {
                    NSLog(@"Error posting the Open Graph object to the Object API: %@", error);
                }
            }];

        } else {
            NSLog(@"Error staging an image: %@", error);
        }
    }];

Got object id as result.

Then create an action and link it with object:

 id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:@"my_object_type"];

FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] init];
params.action = action;
params.actionType = @"myapp:my_app_action";

And post it to Facebook:

 [FBRequestConnection startForPostWithGraphPath:@"/me/myapp:my_app_action" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    if(!error) {
        NSLog(@"OG story posted, story id: %@", [result objectForKey:@"id"]);
        [[[UIAlertView alloc] initWithTitle:@"OG story posted"
                                    message:@"Check your Facebook profile or activity log to see the story."
                                   delegate:self
                          cancelButtonTitle:@"OK!"
                          otherButtonTitles:nil] show];
    } else {
        NSLog(@"Encountered an error posting to Open Graph: %@", error);
    }
}];

As a result i got the message that OG story was successfully posted, but when going to Facebook, there is nothing in feed or timeline, just an image added to users photo album.

1

There are 1 answers

1
r.kolodiy On BEST ANSWER

Well, dunno what was the problem, but now it works with the following code:

FBRequestConnection *connection = [[FBRequestConnection alloc]init];

NSMutableDictionary <FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
object[@"type"] = @"myapp:my_object_type";
object[@"title"] = @"title";
object[@"url"] = @"http://example.com";
object[@"image"] = @"http://image.png";
object[@"description"] = @"description";

FBRequest *objectRequest = [FBRequest requestForPostOpenGraphObject:object];

[connection addRequest:objectRequest completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    if (error) {
        NSLog(@"Error: %@",error.description);
    }
} batchEntryName:@"objectCreate"];


NSMutableDictionary <FBOpenGraphAction> *action = [FBGraphObject openGraphActionForPost];

action[@"buy"] = @"{result=objectCreate:$.id}";
action[@"fb:explicitly_shared"] = @"true";

FBRequest *actionRequest = [FBRequest requestForPostWithGraphPath:@"me/myapp:my_app_action" graphObject:action];

[connection addRequest:actionRequest completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    if (error) {
        NSLog(@"error: %@",error.description);
    } else {
        NSLog(@"posted with id %@", result[@"id"]);
    }
}];

[connection start];