简体   繁体   中英

Facebook iOS SDK - Post Custom Story

I'm trying to do something that seems to be an order of magnitude more difficult than necessary.

I've got an iOS app that is written against a Parse backend. Our app's users can create items to store and they can sometimes generate alerts for these items.

I want to post a simple alert for a user to Facebook.

I've read quite a bit on the Facebook website about integrating Facebook into iOS, but their documentation is confusing and I've seen more than one person on StackOverflow mention that their iOS code is at least sometimes flawed.

Basically, the custom story will include the following items:

  1. Story Title
  2. Story Description (usually not very much text in here)
  3. One or more pictures (I'd settle for 1, but I'd love to be able to post multiples)
  4. If possible, a user-supplied GPS location.

I will admit up front that I'm a developer who is learning Facebook for work purposes, and I am far from being an expert on Facebook.

When I play around in the Facebook app, I can add a status update that can include:

  1. Text
  2. Picture(s)
  3. Friends
  4. What I'm doing
  5. Where I am (or some location)

However, in the Facebook SDK, there appears to be only the following 2 options for posting a status update:

startForPostStatusUpdate:completionHandler:
startForPostStatusUpdate:place:tags:completionHandler:

If I'm missing something there, please let me know.

Secondly, I'm trying to follow Facebook's documentation and sample code to do the following sequence of events:

  1. Upload and stage an image resource
  2. Create an object (in my app, I've registered an "alert" object)
  3. Create an action (in my app, I've registered a "post" action)

Here's the code that I've pieced together from Facebook's website (comments are mostly from other people's code, not my own):

- (void)postToFacebookWithAlert:(PFObject *)alert image:(UIImage *)image {        
    // Stage the image
    [FBRequestConnection startForUploadStagingResourceWithImage:image completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        if (error != nil) {
            // See: https://developers.facebook.com/docs/ios/errors                
        } else {
            // Package image inside a dictionary, inside an array like we'll need it for the object
            NSArray *image              = @[@{@"url": [result objectForKey:@"uri"], @"user_generated" : @"true" }];
            // Create an object
            NSString *caption           = // user-generate content
            NSMutableDictionary<FBOpenGraphObject> *alert;
            alert                       = [FBGraphObject openGraphObjectForPostWithType:@"<myapp>:alert"
                                                                                  title:caption
                                                                                  image:image
                                                                                    url:nil
                                                                            description:alert[@"subtitle"]];
            alert.provisionedForPost    = YES;

            // Post custom object
            [FBRequestConnection startForPostOpenGraphObject:alert completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if (error != nil) {
                    // An error occurred                        
                } else {
                    NSMutableDictionary<FBGraphObject> *post    = [FBGraphObject graphObject];
                    post[@"alert"]                              = result[@"id"];
                    [FBRequestConnection startForPostWithGraphPath:@"me/<myapp>:post" graphObject:post completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                        NSLog(@"startForPostWithGraphPath:graphObject:completionHandler:");
                        if (error != nil) {
                            // An error occurred
                            NSLog(@"Encountered an error posting to Open Graph: %@", error);                                
                        } else {
                            NSLog(@"OG story posted, story id: %@", result[@"id"]);                                
                        }
                    }];
                }
            }];
        }
    }];        
}

I've actually gotten this code to work one time. I'm logged in as my own personal Facebook account. If I go to my activity log, I can see the items that I'm trying to post. If I go to my photos, I can see the photos that I'm trying to upload. If I view my profile from my wife's app/account, it doesn't show up.

Does anyone have working code that does what I want?

Maybe you need to set "fb:explicitly_shared" to "true" and it should be reviewed by Facebook.

After your review pass, you can add the option like below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM