【问题标题】:How to post to a users wall using Facebook SDK如何使用 Facebook SDK 在用户墙上发帖
【发布时间】:2013-03-24 21:20:18
【问题描述】:

我想在 iOS 应用中使用 facebook sdk 将一些文本发布到用户墙上。

现在发布一个开放的图表故事是做到这一点的唯一方法吗?

我发现开放式图表故事真的很奇怪,您只能以“用户 x a y”的格式发布内容,您可以直接在 facebook 上预设 x 和 y,例如用户 ata 吃披萨或用户玩游戏。设置每一个也很费力,因为您必须在外部服务器上为每一个创建一个 .php 对象。

我错过了什么还是有更简单的方法来解决这个问题?

【问题讨论】:

  • 你想发布什么样的文字?
  • 我正在为游戏中的 facebook 编写一个界面,大多数帖子可能会得到用户得分 x 分或类似的东西,但我希望界面足够强大以能够如果需要,可以发布任何内容。

标签: iphone ios facebook facebook-graph-api facebook-ios-sdk


【解决方案1】:

通过浏览 facebook 教程了解一下。

-(void) postWithText: (NSString*) message
           ImageName: (NSString*) image
                 URL: (NSString*) url
             Caption: (NSString*) caption
                Name: (NSString*) name
      andDescription: (NSString*) description
{

    NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                   url, @"link",
                                   name, @"name",
                                   caption, @"caption",
                                   description, @"description",
                                   message, @"message",
                                   UIImagePNGRepresentation([UIImage imageNamed: image]), @"picture",
                                   nil];

    if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound)
    {
        // No permissions found in session, ask for it
        [FBSession.activeSession requestNewPublishPermissions: [NSArray arrayWithObject:@"publish_actions"]
                                              defaultAudience: FBSessionDefaultAudienceFriends
                                            completionHandler: ^(FBSession *session, NSError *error)
        {
             if (!error)
             {
                 // If permissions granted and not already posting then publish the story
                 if (!m_postingInProgress)
                 {
                     [self postToWall: params];
                 }
             }
         }];
    }
    else
    {
        // If permissions present and not already posting then publish the story
        if (!m_postingInProgress)
        {
            [self postToWall: params];
        }
    }
}

-(void) postToWall: (NSMutableDictionary*) params
{
    m_postingInProgress = YES; //for not allowing multiple hits

    [FBRequestConnection startWithGraphPath:@"me/feed"
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error)
     {
         if (error)
         {
             //showing an alert for failure
             UIAlertView *alertView = [[UIAlertView alloc]
                                       initWithTitle:@"Post Failed"
                                       message:error.localizedDescription
                                       delegate:nil
                                       cancelButtonTitle:@"OK"
                                       otherButtonTitles:nil];
             [alertView show];
         }
         m_postingInProgress = NO;
     }];
}

【讨论】:

  • 您好 Tiddly,这里是 m_postingInProgress。你能详细说明一下吗?我想给朋友墙发消息.....
  • 只是一个简单的标签,用来判断一个帖子是否已经在发,防止多发和冲突。
  • 我只想通过 Facebook 将消息发送给其他用户,而不是发布到他的墙上。但是现在我在朋友的墙上发帖,这根据我的要求是不正确的。
  • 我不知道如何帮助你。您应该发布一个新问题,因为您的问题与我的不同。如果您在 cmets 中发布指向该问题的链接,我会看看是否可以提供帮助。您需要在问题中发布一些源代码,以便人们能够帮助您。
  • 只是一个注释。您可以更简单地使用 startForPostWithGraphPath,或者如果它只是一张照片,只需使用 startForUploadPhoto
【解决方案2】:

从您的 iOS 应用程序共享内容的最简单方法是使用 UIActivityViewController 类,here 您可以找到该类的文档,here 是一个很好的使用示例。很简单:

NSString *textToShare = @”I just shared this from my App”;
UIImage *imageToShare = [UIImage imageNamed:@"Image.png"];
NSURL *urlToShare = [NSURL URLWithString:@"http://www.bronron.com"];
NSArray *activityItems = @[textToShare, imageToShare, urlToShare];

UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:TRUE completion:nil];

这仅适用于 iOS 6,它使用用户设置中配置的 Facebook 帐户,不需要 Facebook SDK。

【讨论】:

  • 这似乎正是我想要的,但后来我发现它只适用于 iOS 6 及更高版本:( 我需要至少以 iOS 5 为目标。谢谢。
  • 我只是将最后一条评论添加到我的答案中;)
  • 翻看一个老项目,发现我用这个项目发到facebook,兼容iOS4,看看吧,也许这就是你需要的github.com/sakrist/FacebookSample
【解决方案3】:

您也可以使用 Graph API

完成使用 iOS 创建 facebook 应用程序的所有基本步骤后,您就可以开始享受 Graph API 的功能了。下面的代码将发布“hello world!”在你的墙上:

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>

...

//to get the permission 
//https://developers.facebook.com/docs/facebook-login/ios/permissions  
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]) {
            NSLog(@"publish_actions is already granted.");
        } else {
            FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
            [loginManager logInWithPublishPermissions:@[@"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
                //TODO: process error or result.
            }];
        }

    if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]) {
        [[[FBSDKGraphRequest alloc]
          initWithGraphPath:@"me/feed"
          parameters: @{ @"message" : @"hello world!"}
          HTTPMethod:@"POST"]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSLog(@"Post id:%@", result[@"id"]);
             }
         }];
    }
...

这里介绍的是基本人员:https://developers.facebook.com/docs/ios/graph

可以玩的探索者在这里: https://developers.facebook.com/tools/explorer

一个很好的介绍:https://www.youtube.com/watch?v=WteK95AppF4

【讨论】:

  • 我似乎无法找到有关 parameters 可能条目的文档。 (例如:您可以发布图片吗?网址?等。如果可以,键是什么?值?)谢谢!
  • 在这里您可以找到可能的参数条目:developers.facebook.com/docs/graph-api/reference
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多