8

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

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

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

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

4

3 回答 3

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;
     }];
}
于 2013-04-03T15:14:24.130 回答
4

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

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。

于 2013-04-03T09:27:55.197 回答
4

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

于 2015-04-21T01:09:11.137 回答