Facebook使用图形api发布到墙上的观众选择器

时间:2016-02-15 15:31:41

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

我已经在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只发布给我,这对我不起作用。 另一方面,它发布为朋友。 请任何人帮助我以编程方式仅发布我和每个人。 如果我出错了,请指导我。

提前致谢!

1 个答案:

答案 0 :(得分: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上发布到您的Feed。

对于其他受众,您必须在此处获得隐私价值,而不是'SELF'。

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