如何使用Facebook SDK发布到用户墙

时间:2013-04-03 09:22:51

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

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

现在发布一个开放的图形故事是唯一的方法吗?

我发现在开放图形故事中他们真的很奇怪,你只能以“用户x甚至”的格式发布信息,你可以直接在Facebook上预设x和y,比如用户在比萨饼或用户玩游戏。设置每一个也非常费力,因为你必须在外部服务器上为每个对象创建一个.php对象。

我错过了什么或有更简单的方法可以解决这个问题吗?

3 个答案:

答案 0 :(得分:10)

通过浏览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;
     }];
}

答案 1 :(得分:4)

从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。

答案 2 :(得分:4)

您也可以使用图谱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