Search code examples
iosobjective-cfacebookfacebook-graph-apifacebook-checkins

How to post checkin on Facebook?


I need to checkin on Facebook, without using the default checkin option that comes with Facebook iOS SDK, because I need to filter the Facebook places, so that user can only checkin using places I filtered. I have tried this using Facebook graph api.

NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:accsstoken,@"access_token",@"253651184683030",@"place",@"I m here in this place",@"message",@"30.893075018178,75.821777459326",@"coordinates", nil];

[FBRequestConnection startWithGraphPath:@"/me/checkins"
                             parameters:dict
                             HTTPMethod:@"POST"
                      completionHandler:^(
                                          FBRequestConnection *connection,
                                          id result,
                                          NSError *error
                                          ) {
                          NSLog(@"Error...%@",error);

                         }]; 

if anybody know about this, Do let me know ? Any kind of help will be appreciated.


Solution

  • Here is a working code:

    _place.facebookId = place id of location to checkin in

    listFriends = a list of friend's id to checkin with

    -(void)publishToFacebook:(NSString *)message
    {
    // Create the parameters dictionary that will keep the data that will be posted.
    NSMutableArray *fArray = [NSMutableArray new];
    for (RCPerson *person in listFriends)
    {
        [fArray addObject:person.ID];
    }
    
    NSMutableDictionary * params = [NSMutableDictionary new];
    
    if(fArray.count > 0)
    {
        params = [NSMutableDictionary dictionaryWithObjectsAndKeys: message, @"message",[fArray componentsJoinedByString:@","], @"tags",  _place.facebookId, @"place", nil];
    
    }
    else
    {
        params = [NSMutableDictionary dictionaryWithObjectsAndKeys: message, @"message", _place.facebookId, @"place", nil];
    
    }
    
    NSLog(@"params %@", params);
    FBRequest *postRequest = [FBRequest requestWithGraphPath:@"me/feed" parameters:params HTTPMethod:@"POST"];
    postRequest.session = FBSession.activeSession;
    if(![postRequest.session.permissions containsObject:@"publish_stream"])
    {
    
        [postRequest.session requestNewPublishPermissions:@[@"publish_stream", @"publish_actions"] defaultAudience:FBSessionDefaultAudienceEveryone completionHandler:^(FBSession *session, NSError *error) {
            [postRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                NSLog(@"error %@", error.description);
                if(error)
                {
                    dispatch_async(dispatch_get_main_queue(), ^(void) {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook error" message:[self parseError:error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                        [alert show];
                    });
                }
                NSLog(@"%@", result);
    
                //[self checkinMe];
    
            }];
        }];
    }
    else
    {
        [postRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            NSLog(@"error %@", error.description);
            if(error)
            {
    
                dispatch_async(dispatch_get_main_queue(), ^(void) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook error" message:[self parseError:error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert show];
                });
            }
            NSLog(@"%@", result);
            //[self checkinMe];
    
        }];
    }
    
    }