如何使用 iPhone 版 Facebook Graph API 向朋友墙发帖

发布于 2024-10-11 03:26:46 字数 1405 浏览 1 评论 0原文

我想在用户朋友墙上发布一些内容。

我用它来发布到用户墙,

SBJSON *jsonWriter = [[SBJSON new] autorelease];

    NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           @"Always Running",@"text",@"http://itsti.me/",@"href", nil], nil];

    NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
    NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"a long run", @"name",
                                @"The Facebook Running app", @"caption",
                                @"it is fun", @"description",
                                @"http://itsti.me/", @"href", nil];
    NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"Share on Facebook",  @"user_message_prompt",
                                   actionLinksStr, @"action_links",
                                   attachmentStr, @"attachment",nil];

    [facebook dialog:@"feed"
            andParams:params
          andDelegate:self];

但我需要发布到我的朋友墙。我该如何做到这一点

在此帖子他们提到了 targetId 我是否必须对此做些什么。

I want to post something in the users friends wall.

I use this to post into the user wall

SBJSON *jsonWriter = [[SBJSON new] autorelease];

    NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           @"Always Running",@"text",@"http://itsti.me/",@"href", nil], nil];

    NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
    NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"a long run", @"name",
                                @"The Facebook Running app", @"caption",
                                @"it is fun", @"description",
                                @"http://itsti.me/", @"href", nil];
    NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"Share on Facebook",  @"user_message_prompt",
                                   actionLinksStr, @"action_links",
                                   attachmentStr, @"attachment",nil];

    [facebook dialog:@"feed"
            andParams:params
          andDelegate:self];

But i need to post in my friends wall. how can i do this

In this post they have mentioned about targetId do i have to do something with that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

廻憶裏菂餘溫 2024-10-18 03:26:46

您是否尝试过使用,

[facebook requestWithGraphPath:@"[friend_ID]/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];

更新:

在朋友墙上发帖将不再适用于 Graph API。相反,您必须使用 FBDialog。

Have you tried using,

[facebook requestWithGraphPath:@"[friend_ID]/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];

UPDATE:

Posting on friends wall will no more work with Graph API. Instead you must use FBDialog.

土豪我们做朋友吧 2024-10-18 03:26:46

@mAc

你必须先授权facebook。然后,您在类中实现“FBRequestDelegate”方法。然后使用图形 API 发出请求,如下所示

[facebook requestWithGraphPath:@"/me/friends" andDelegate:self];

一旦请求成功,API 将调用委托方法“requestDdidLoad”,

-(void)request:(FBRequest *)request didLoad:(id)result {

    NSLog(@"Result: %@", result);

}

从结果中您将获得 Frieds 页面 ID

@mAc

You have to authorize facebook first. Then you implement 'FBRequestDelegate' methods to your class. Then make a request with graph API as given below

[facebook requestWithGraphPath:@"/me/friends" andDelegate:self];

Once your request succeeded, api will call the delegate method 'requestDdidLoad',

-(void)request:(FBRequest *)request didLoad:(id)result {

    NSLog(@"Result: %@", result);

}

Form the result you will get the frieds page ID

眼泪都笑了 2024-10-18 03:26:46
- (IBAction)InviteAction:(id)sender  // Button action 
{
    if (!FBSession.activeSession.isOpen) {
        // if the session is closed, then we open it here, and establish a handler for state changes
        [FBSession openActiveSessionWithReadPermissions:nil
                                           allowLoginUI:YES
                                      completionHandler:^(FBSession *session,
                                                          FBSessionState state,
                                                          NSError *error) {
                                          if (error) {
                                              UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Invite friends process cancelled"
                                                                                                  message:nil
                                                                                                 delegate:nil
                                                                                        cancelButtonTitle:@"OK"
                                                                                        otherButtonTitles:nil];
                                              [alertView show];
                                          } else if (session.isOpen) {
                                              [self InviteAction:sender];
                                          }
                                      }];
        return;
    }

    if (self.friendPickerController == nil) {
        // Create friend picker, and get data loaded into it.
        self.friendPickerController = [[FBFriendPickerViewController alloc] init];
        self.friendPickerController.title = @"Pick Friends";
        self.friendPickerController.delegate = self;
    }

    [self.friendPickerController loadData];
    [self.friendPickerController clearSelection];

    [self presentViewController:self.friendPickerController animated:YES completion:nil];
}

- (void) performPublishAction:(void (^)(void)) action
{
    if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound)
    {
        [FBSession.activeSession requestNewPublishPermissions:@[@"publish_actions"]
                                              defaultAudience:FBSessionDefaultAudienceFriends
                                            completionHandler:^(FBSession *session, NSError *error) {
                                                if (!error) {
                                                    action();
                                                } else if (error.fberrorCategory != FBErrorCategoryUserCancelled){
                                                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Permission denied"
                                                                                                        message:@"Unable to get permission to post"
                                                                                                       delegate:nil
                                                                                              cancelButtonTitle:@"OK"
                                                                                              otherButtonTitles:nil];
                                                    [alertView show];
                                                }
                                            }];
    } else {
        action();
    }

}



- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user
{
    self.loggedInUser = user;
}


- (void)facebookViewControllerDoneWasPressed:(id)sender
{
    NSMutableString *text = [[NSMutableString alloc] init];
    for (id<FBGraphUser> user in self.friendPickerController.selection)
    {

        if ([text length]) {
            [text appendString:@","];
        }
        [text appendString:[NSString stringWithFormat:@"%@",user.id]];
    }

    //For post to friend's wall
    NSDictionary *params = @{
                            @"name" : @"Hello Please checkout this app",
                             @"caption" : @" IOS APP",
                            @"description" : @"",
                             @"picture" : @"[email protected]",
                             @"link" : @"http:www.google.com",
                             @"to":text,

                             };


    // Invoke the dialog
    [FBWebDialogs presentFeedDialogModallyWithSession:nil
                                          parameters:params
                                             handler:
     ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
         if (error) {
             NSLog(@"Error publishing story.");
             UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:@"Failed" message:@"Failed to Post" delegate:Nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
             [alertshow show];
         } else {
             if (result == FBWebDialogResultDialogNotCompleted)
             {
                NSLog(@"User canceled story publishing.");
                 UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:@"Failed"   message:@"Failed to post on your friend wall" delegate:Nil  cancelButtonTitle:@"ok" otherButtonTitles:nil];
                [alertshow show];
             } else {
                 NSLog(@"Story published.");
                 UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Posted on Friend wall" delegate:Nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
                 [alertshow show];
            }
         }}];



    [self fillTextBoxAndDismiss:text.length > 0 ? text : @"<None>"];
}

- (void)facebookViewControllerCancelWasPressed:(id)sender {
    [self fillTextBoxAndDismiss:@"<Cancelled>"];
}

- (void)fillTextBoxAndDismiss:(NSString *)text
{
    [self dismissModalViewControllerAnimated:YES];
}
- (IBAction)InviteAction:(id)sender  // Button action 
{
    if (!FBSession.activeSession.isOpen) {
        // if the session is closed, then we open it here, and establish a handler for state changes
        [FBSession openActiveSessionWithReadPermissions:nil
                                           allowLoginUI:YES
                                      completionHandler:^(FBSession *session,
                                                          FBSessionState state,
                                                          NSError *error) {
                                          if (error) {
                                              UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Invite friends process cancelled"
                                                                                                  message:nil
                                                                                                 delegate:nil
                                                                                        cancelButtonTitle:@"OK"
                                                                                        otherButtonTitles:nil];
                                              [alertView show];
                                          } else if (session.isOpen) {
                                              [self InviteAction:sender];
                                          }
                                      }];
        return;
    }

    if (self.friendPickerController == nil) {
        // Create friend picker, and get data loaded into it.
        self.friendPickerController = [[FBFriendPickerViewController alloc] init];
        self.friendPickerController.title = @"Pick Friends";
        self.friendPickerController.delegate = self;
    }

    [self.friendPickerController loadData];
    [self.friendPickerController clearSelection];

    [self presentViewController:self.friendPickerController animated:YES completion:nil];
}

- (void) performPublishAction:(void (^)(void)) action
{
    if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound)
    {
        [FBSession.activeSession requestNewPublishPermissions:@[@"publish_actions"]
                                              defaultAudience:FBSessionDefaultAudienceFriends
                                            completionHandler:^(FBSession *session, NSError *error) {
                                                if (!error) {
                                                    action();
                                                } else if (error.fberrorCategory != FBErrorCategoryUserCancelled){
                                                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Permission denied"
                                                                                                        message:@"Unable to get permission to post"
                                                                                                       delegate:nil
                                                                                              cancelButtonTitle:@"OK"
                                                                                              otherButtonTitles:nil];
                                                    [alertView show];
                                                }
                                            }];
    } else {
        action();
    }

}



- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user
{
    self.loggedInUser = user;
}


- (void)facebookViewControllerDoneWasPressed:(id)sender
{
    NSMutableString *text = [[NSMutableString alloc] init];
    for (id<FBGraphUser> user in self.friendPickerController.selection)
    {

        if ([text length]) {
            [text appendString:@","];
        }
        [text appendString:[NSString stringWithFormat:@"%@",user.id]];
    }

    //For post to friend's wall
    NSDictionary *params = @{
                            @"name" : @"Hello Please checkout this app",
                             @"caption" : @" IOS APP",
                            @"description" : @"",
                             @"picture" : @"[email protected]",
                             @"link" : @"http:www.google.com",
                             @"to":text,

                             };


    // Invoke the dialog
    [FBWebDialogs presentFeedDialogModallyWithSession:nil
                                          parameters:params
                                             handler:
     ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
         if (error) {
             NSLog(@"Error publishing story.");
             UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:@"Failed" message:@"Failed to Post" delegate:Nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
             [alertshow show];
         } else {
             if (result == FBWebDialogResultDialogNotCompleted)
             {
                NSLog(@"User canceled story publishing.");
                 UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:@"Failed"   message:@"Failed to post on your friend wall" delegate:Nil  cancelButtonTitle:@"ok" otherButtonTitles:nil];
                [alertshow show];
             } else {
                 NSLog(@"Story published.");
                 UIAlertView *alertshow = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Posted on Friend wall" delegate:Nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
                 [alertshow show];
            }
         }}];



    [self fillTextBoxAndDismiss:text.length > 0 ? text : @"<None>"];
}

- (void)facebookViewControllerCancelWasPressed:(id)sender {
    [self fillTextBoxAndDismiss:@"<Cancelled>"];
}

- (void)fillTextBoxAndDismiss:(NSString *)text
{
    [self dismissModalViewControllerAnimated:YES];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文