【问题标题】:Facebook post to wall with audience selector using graph api使用图形 api 使用受众选择器将 Facebook 发布到墙上
【发布时间】:2016-02-15 15:31:41
【问题描述】:

我已经用 Objective-C 编写了这段代码

#import <FacebookSDK/FacebookSDK.h>

#import "CustomLoginViewController.h"
#import "AppDelegate.h"

@implementation CustomLoginViewController

- (IBAction)buttonTouched:(id)sender
{

    if (FBSession.activeSession.state == FBSessionStateOpen
        || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

        [FBSession.activeSession closeAndClearTokenInformation];

    } else {

        [FBSession openActiveSessionWithPublishPermissions:@[@"publish_actions"] defaultAudience:FBSessionDefaultAudienceOnlyMe allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {

            __block NSString *alertText;
            __block NSString *alertTitle;
            if (!error) {
                if ([FBSession.activeSession.permissions
                     indexOfObject:@"publish_actions"] == NSNotFound){
                    // Permission not granted, tell the user we will not publish
                    alertTitle = @"Permission not granted";
                    alertText = @"Your action will not be published to Facebook.";
                    [[[UIAlertView alloc] initWithTitle:alertTitle
                                                message:alertText
                                               delegate:self
                                      cancelButtonTitle:@"OK!"
                                      otherButtonTitles:nil] show];
                } else {
                    // Permission granted, publish the OG story
                    //                                                    [self publishStory];
                    NSLog(@"Publish permission granted !");
                    [self publishPOST];
                }

            } else {
                // There was an error, handle it
                // See https://developers.facebook.com/docs/ios/errors/
            }


            // Retrieve the app delegate
            AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
            // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
            [appDelegate sessionStateChanged:session state:status error:error];
        }];
    }//else
}
- (IBAction)post:(id)sender {

}
-(void)publishPOST{
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"Description goes here.",@"description",
                                   @"www.google.com/image.jpg",@"picture",
                                   @"www.google.com/invites/username", @"link",
                                   @"Google",@"name",
                                   @"www.google.com",@"caption",
                                   nil];

    [FBRequestConnection startWithGraphPath:@"me/feed" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            NSLog(@"published !");
        } else {
            NSLog(@"published Failed ??");
        }
    }];

}

@end

但问题是我想用 FBSessionDefaultAudienceNone 发帖, FBSessionDefaultAudienceOnlyMe、FBSessionDefaultAudienceFriends 和 FBSessionDefaultAudienceEveryone 以编程方式。

我已经写了 FBSessionDefaultAudienceOnlyMe 以仅作为我发布,它对我不起作用。 另一方面,它作为朋友发布。 请任何人帮助我以编程方式仅以我和每个人的身份发布。 如果我错了,请指导我。

提前致谢!

【问题讨论】:

    标签: ios objective-c facebook facebook-graph-api


    【解决方案1】:

    你可以试试这个代码,这可能会对你有所帮助

    - (void)postData:(NSString *)message url:(NSURL *)url thumbnail:(NSURL *)thumbnail audience:(SocialNetworkAudience)audience
    {        
        NSString* privacy;
        switch (audience) {
            case SocialNetworkAudiencePrivate:
                privacy = @"SELF";
                break;
            case SocialNetworkAudienceProtected:
                privacy = @"ALL_FRIENDS";
                break;
            case SocialNetworkAudiencePublic:
                privacy = @"EVERYONE";
                break;
        }
    
        [FBRequestConnection
            startWithGraphPath:@"/me/feed"
            parameters:@{
                @"description": message,
                @"link": url.absoluteString,
                @"picture": thumbnail.absoluteString,
                @"privacy": @"{'value': 'SELF'}"
            }
            HTTPMethod:@"POST"
            completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            }
        ];
    }
    

    这将发布到您在 facebook 中的提要中。

    对于其他受众,您必须在此行中获得隐私价值来代替“SELF”。

    @"privacy": @"{'value': 'SELF'}" 
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多