2Grey
2Grey

Reputation: 177

Facebook: Share to specific people or list from iOS app

If you share the news to Facebook on your computer you have the options to share for "Specific people or list".

I need to share photo from my iOS app only for some list of friends.

Is it possible to share news only for "Specific people or list" from app using Graph API?

Upvotes: 2

Views: 174

Answers (1)

Ramdhas
Ramdhas

Reputation: 1765

For post on user's wall using Social Framework

in ACFacebookAudienceKey, choose one of these

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]);
            }];


        }
    }
}];

For post to friend's wall.

- (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];
}

Upvotes: 1

Related Questions