代码之家  ›  专栏  ›  技术社区  ›  Tiddly

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

  •  8
  • Tiddly  · 技术社区  · 11 年前

    我想在iOS应用程序中使用facebook sdk向用户墙发布一些文本。

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

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

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

    3 回复  |  直到 11 年前
        1
  •  10
  •   Tiddly    11 年前

    通过浏览更多的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;
         }];
    }
    
        2
  •  4
  •   neowinston    10 年前

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

        3
  •  4
  •   Darius Miliauskas    9 年前

    您可以使用 图API

    在完成所有使用iOS创建脸书应用程序的基本步骤后,您可以开始享受Graph API的功能。下面的代码将在你的墙上张贴“你好,世界!”:

    #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