iosfacebook-graph-apiphoto-upload

Photo Upload to own wall via iOS Graph API with user's location


I need to post a photo (taken form the camera) in the users facebook wall with the location of the user. Now, the facebook photo object has a field named place:

object containing id and name of Page associated with this location, and a location field containing geographic information such as latitude, longitude, country, and other fields (fields will vary based on geography and availability of information)

Now how do I get the place, attach it with the photo and upload it in the users wall. This is my code for photo upload:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       resultImage, @"picture",
                                       location, @"place",
                                       nil];

        [appDelegate.facebook requestWithGraphPath:@"me/photos"
                                         andParams:params
                                     andHttpMethod:@"POST"
                                       andDelegate:self];

But, how do I get the location parameter here? Can anyone help? Thanks in advance.


Solution

  • The place object can only be a Facebook Page Id of the desired location according to this documentation. So, here is how I managed to get user's location while uploading photo.

    -(void)fbCheckForPlace:(CLLocation *)location
    {
        NSString *centerLocation = [[NSString alloc] initWithFormat:@"%f,%f",
                                    location.coordinate.latitude,
                                    location.coordinate.longitude];
        NSLog(@"center location is : %@",centerLocation);
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        @"place",  @"type",
                                        centerLocation, @"center",
                                        @"1000",  @"distance",
                                        nil];
        [centerLocation release];
        [appDelegate.facebook requestWithGraphPath:@"search" andParams:params andDelegate:self];
    }
    

    the current location is obtained by calling CLLocation Manager delegate and passed in the above method.

    Next, if this request is successful, the place objects are inserted in a mutable array.

    NSArray *resultData = [result objectForKey:@"data"];
            for (NSUInteger i=0; i<[resultData count] && i < 5; i++) 
            {
                [self.listOfPlaces addObject:[resultData objectAtIndex:i]];
            }
    

    And then the photo uploading method is fired:

    - (void)uploadPhotoWithLocation
    {
        NSString *placeId = [[self.listOfPlaces objectAtIndex:0] objectForKey:@"id"];
    
        params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       self.resultImage, @"picture",
                                       placeId, @"place",
                                       nil];
    
        [appDelegate.facebook requestWithGraphPath:@"me/photos"
                                         andParams:params
                                     andHttpMethod:@"POST"
                                       andDelegate:self];
    }
    

    I have taken the first nearby place of the available check-ins ([self.listOfPlaces objectAtIndex:0]) and now the app can successfully post photos with the users current nearby location.