如何在Swift中将故事发布到Facebook? [英] How do I post a story to Facebook in Swift?

查看:82
本文介绍了如何在Swift中将故事发布到Facebook?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处搜索,找不到关于如何在Swift中将故事发布到Facebook的文档.我试图将这段代码从Obj-C转换为Swift,但是并没有取得太大进展(我不知道如何在Obj-C中进行编码).我希望在Swift中完成以下操作: https://developers.facebook.com/docs/ios/graph#postingstory

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

以下是相关代码:

// 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    
                           }
                         }];

基本上,我正在寻找这段代码的Swift翻译.在我的实际应用中,我将为游戏(而不是喜欢餐馆)发布高分,但是我应该能够弄清楚是否可以使用Swift.

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.

提前谢谢!

推荐答案

根据要求,这是我最终使用的方法.它不使用FBOpenGraphAction,这使我的实现更简单. (注意:您需要使用FBLoginViewDelegate):

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)

这篇关于如何在Swift中将故事发布到Facebook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆