How to send Facebook story to Swift?

I searched everywhere and I cannot find documentation on how to post a story on Facebook in Swift. I tried to translate this code from Obj-C to Swift, but I didn't make much progress (I don't know how to code in Obj-C). I want to do something similar 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 application, I am going to post a high score for the game (I do not like the restaurant), but I should be able to understand this if I have Swift to work.

Thanks in advance!

+4
source share
1 answer

, , . FBOpenGraphAction, . (: 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)
+1

All Articles