Facebook:与特定用户分享或从iOS应用列表

时间:2014-04-16 07:02:57

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

如果您在计算机上向Facebook分享新闻,您可以选择分享“特定人员或列表”。

我需要从我的iOS应用程序中分享照片,仅用于一些朋友列表。

是否可以使用Graph API从应用分享“特定人员或列表”的新闻?

1 个答案:

答案 0 :(得分:1)

使用社交框架在用户墙上发帖

在ACFacebookAudienceKey中,选择其中一个

<强> 1.ACFacebookAudienceEveryone

<强> 2.ACFacebookAudienceFriends

<强> 3.ACFacebookAudienceOnlyMe

ACAccountStore *accountStore = [[ACAccountStore alloc] init];

ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; NSLog(@"0");

[accountStore requestAccessToAccountsWithType:accountType options:@{ACFacebookAppIdKey : @"00000000000", ACFacebookPermissionsKey : @"publish_stream", ACFacebookAudienceKey : ACFacebookAudienceFriends} completion:^(BOOL granted, NSError *error) {
    if(granted) {
        NSLog(@"1");
        NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
        NSLog(@"2");
        if ([accountsArray count] > 0) {
            NSLog(@"3");
            ACAccount *facebookAccount = [accountsArray objectAtIndex:0];
            NSLog(@"4");
            SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                            requestMethod:SLRequestMethodPOST
                                                                      URL:[NSURL URLWithString:@"https://graph.facebook.com/me/feed"]
                                                               parameters:[NSDictionary dictionaryWithObject:post forKey:@"message"]];
            NSLog(@"5");

            [facebookRequest setAccount:facebookAccount];
            NSLog(@"6");

            [facebookRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
                NSLog(@"%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
            }];


        }
    }
}];

发帖给朋友的墙。

- (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" : @"logo@2x.png",
                             @"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];
}