Micah Stairs
Micah Stairs

Reputation: 235

How do I post a story to Facebook in Swift?

I have searched everywhere and I'm unable to find any documentation on how to post a story to Facebook in Swift. I have tried to translate this code from Obj-C to Swift, but I haven't made much progress (I do not know how to code in Obj-C). I am looking to accomplish something like this in Swift: https://developers.facebook.com/docs/ios/graph#postingstory

Here is the relevant code:

// Create a like action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];

// Link that like action to the restaurant object that we have created
[action setObject:_objectID forKey:@"object"];

// Post the action to Facebook
[FBRequestConnection startForPostWithGraphPath:@"me/og.likes"
                               graphObject:action
                         completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                           __block NSString *alertText;
                           __block NSString *alertTitle;
                           if (!error) {
                             // Success, the restaurant has been liked
                             NSLog(@"Posted OG action, id: %@", [result objectForKey:@"id"]);
                             alertText = [NSString stringWithFormat:@"Posted OG action, id: %@", [result objectForKey:@"id"]];
                             alertTitle = @"Success";
                             [[[UIAlertView alloc] initWithTitle:alertTitle
                                                         message:alertText
                                                        delegate:self
                                               cancelButtonTitle:@"OK!"
                                               otherButtonTitles:nil] show];
                           } else {
                             // An error occurred, we need to handle the error
                             // See: https://developers.facebook.com/docs/ios/errors    
                           }
                         }];

Essentially I'm looking for a Swift translation of this piece of code. In my actual app, I'm going to be posting a high score for a game (not liking a restaurant), but I should be able to figure that out if I have some Swift to work with.

Thanks in advance!

Upvotes: 4

Views: 2536

Answers (1)

Micah Stairs
Micah Stairs

Reputation: 235

As requested, here was the approach I ended up using. It doesn't use the FBOpenGraphAction, which made it a lot more simple to implement in my case. (Note: you need to use the FBLoginViewDelegate):

    // Initialize variables
    let name = "Purple Square"
    let link = "https://itunes.apple.com/us/app/purple-square/id942125866?ls=1&mt=8"
    let description = String(format: "I got %.1f. Can you beat me?", highScore)
    let picture = "https://www.dropbox.com/s/2nbaxai1arhqqrn/purpleSquareSplash.png?dl=1"
    let caption = "Exceptionally simple. Deceivingly hard. Ridiculously addictive."

    // Add variables to dictionary
    var dict = NSMutableDictionary()
    dict.setValue(name, forKey: "name")
    dict.setValue(caption, forKey: "caption")
    dict.setValue(description, forKey: "description")
    dict.setValue(link, forKey: "link")
    dict.setValue(picture, forKey: "picture")

    // Present the feed dialog and post the story
    FBWebDialogs.presentFeedDialogModallyWithSession(nil, parameters: dict, handler: nil)

Upvotes: 1

Related Questions