iOS - Facebook Open Graph Api — ensuring that phot

2019-06-02 15:52发布

问题:

I am attempting to achieve the following:

This necessitates the open graph request to look something like this: (https://developers.facebook.com/docs/opengraph/howtos/adding-photos-to-stories/)

POST /me/cookbook:eat?
  recipe=http://www.example.com/recipes/pizza/&
  image[0][url]=http://www.example.com/recipes/pizza/pizza.jpg&
  image[0][user_generated]=true&
  image[1][url]=http://www.example.com/recipes/pizza/pizza2.jpg&
  image[1][user_generated]=true&
  access_token=VALID_ACCESS_TOKEN

However, using this code: (from the scruptious tutorial here: https://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/publish-open-graph-story/ ) I do not know how to set the user generated flag to true. Because of this, the photo appears small.

- (void)postOpenGraphActionWithPhotoURL:(NSString*)photoURL
{
    // First create the Open Graph meal object for the meal we ate.
    id<SCOGMeal> mealObject = [self mealObjectForMeal:self.selectedMeal];

    // Now create an Open Graph eat action with the meal, our location, 
    // and the people we were with.
    id<SCOGEatMealAction> action = 
        (id<SCOGEatMealAction>)[FBGraphObject graphObject];
    action.meal = mealObject;
    if (self.selectedPlace) {
        action.place = self.selectedPlace;
    }
    if (self.selectedFriends.count > 0) {
        action.tags = self.selectedFriends;
    }
    if (photoURL) {
        NSMutableDictionary *image = [[NSMutableDictionary alloc] init];
        [image setObject:photoURL forKey:@"url"];

        NSMutableArray *images = [[NSMutableArray alloc] init];
        [images addObject:image];

        action.image = images;
    }

    // Create the request and post the action to the 
    // "me/<YOUR_APP_NAMESPACE>:eat" path.
    [FBRequestConnection startForPostWithGraphPath:@"me/<YOUR_APP_NAMESPACE>:eat"
                             graphObject:action
                       completionHandler:
     ^(FBRequestConnection *connection, id result, NSError *error) {
         NSString *alertText;
         if (!error) {
             alertText = [NSString stringWithFormat:
                             @"Posted Open Graph action, id: %@",
                             [result objectForKey:@"id"]];
         } else {
             alertText = [NSString stringWithFormat:
                             @"error: domain = %@, code = %d",
                             error.domain, error.code];
         }
         [[[UIAlertView alloc] initWithTitle:@"Result" 
                                     message:alertText 
                                    delegate:nil 
                           cancelButtonTitle:@"Thanks!" 
                           otherButtonTitles:nil] 
          show]; 
     }
     ];
}

here, mealobject conforms to the FBGraphObject protocol. Using the same protocol that I am inheriting from, how do I set the user generated flag to be true? The documentation makes no mention of anything "user_generated".

Or should I not use the protocol and manually format the string as per the desired post parameters?

EDIT: I tried to manually do this using a string instead of the FBOpenGraph object, and I succeeded in replicating this same functionality.

However, I have been unable to get multiple photos to appear or set them to be user generated.

SCProtocols.h:

#import <Foundation/Foundation.h>
#import <FacebookSDK/FacebookSDK.h>

@protocol SCOGMeal<FBGraphObject>

@property (retain, nonatomic) NSString *id;
@property (retain, nonatomic) NSString *url;

@end


@protocol SCOGEatMealAction<FBOpenGraphAction>

@property (retain, nonatomic) id<SCOGMeal> meal;

@end

回答1:

You should be able to replace your code:

if (photoURL) {
    NSMutableDictionary *image = [[NSMutableDictionary alloc] init];
    [image setObject:photoURL forKey:@"url"];

    NSMutableArray *images = [[NSMutableArray alloc] init];
    [images addObject:image];

    action.image = images;
}

with:

if (photoURL) {
    NSMutableDictionary *image = [[NSMutableDictionary alloc] init];
    [image setObject:photoURL forKey:@"url"];

    [image setObject:@"true" forKey:@"user_generated"]; // <======= ***add this line***

    NSMutableArray *images = [[NSMutableArray alloc] init];
    [images addObject:image];

    action.image = images;
}