Can we get user zip code using facebook sdk?

1.5k Views Asked by At

I am using facebook sdk 3.0

At facebook i have added users full address as

Address- 1, Infinite loop

city/town - Cupertino, CA

UPDATE

As per the answer I tried to use FBGraphLocation protocol like this

 NSDictionary<FBGraphUser> *user
 id<FBGraphPlace> objPlace = user.location;
 id<FBGraphLocation> objLocation = objPlace.location;

 NSLog(@"place.location : %@ location.street : %@",objPlace.location,objLocation.street);

It is giving (null) for both. also I am getting objLocation object as (null)

In facebook sdk response, I am getting location as only this

location =     {
        id = 123456789123456;
        name = "Cupertino, California";
    }

This is not the enough information from where i can get the zipcode of the use.

If facebook does not allow to provide zip code information then any way to get zipcode from only "Cupertino, California" information ?

I have used this url

http://maps.googleapis.com/maps/api/geocode/json?address=Cupertino,%20California&sensor=true

which give me result as

{
        "long_name" = Cupertino;
        "short_name" = Cupertino;
        types =         (
            locality,
            political
        );
    },
        {
        "long_name" = "Santa Clara";
        "short_name" = "Santa Clara";
        types =         (
            "administrative_area_level_2",
            political
        );
    },
        {
        "long_name" = California;
        "short_name" = CA;
        types =         (
            "administrative_area_level_1",
            political
        );
    },
        {
        "long_name" = "United States";
        "short_name" = US;
        types =         (
            country,
            political
        );
    }

If I statically put that address into the URL as

http://maps.googleapis.com/maps/api/geocode/json?address=1%20Infinite%20Loop,%20Cupertino,%20CA&sensor=true

Then It gives me Zip code also.

{
        "long_name" = 1;
        "short_name" = 1;
        types =         (
            "street_number"
        );
    },
        {
        "long_name" = "Apple Inc.";
        "short_name" = "Apple Inc.";
        types =         (
            establishment
        );
    },
        {
        "long_name" = "Infinite Loop";
        "short_name" = "Infinite Loop";
        types =         (
            route
        );
    },
        {
        "long_name" = Cupertino;
        "short_name" = Cupertino;
        types =         (
            locality,
            political
        );
    },
        {
        "long_name" = "Santa Clara";
        "short_name" = "Santa Clara";
        types =         (
            "administrative_area_level_2",
            political
        );
    },
        {
        "long_name" = California;
        "short_name" = CA;
        types =         (
            "administrative_area_level_1",
            political
        );
    },
        {
        "long_name" = "United States";
        "short_name" = US;
        types =         (
            country,
            political
        );
    },
        {
        "long_name" = 95014;
        "short_name" = 95014;
        types =         (
            "postal_code"
        );
    }

But from the Facebook sdk, I am only getting the City/Town (as stated above), added in contact Information at Facebook.com

How can I do this?

2

There are 2 best solutions below

0
On BEST ANSWER

Finally I used google API to get the latitude and longitude from the address I get from the facebook and using those coordinates I find the zip code.

- (CLLocation *) getGeoCoordinatesUsingAddress: (NSString *) aStrAddres
{

    NSString *esc_addr = [aStrAddres stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    NSString *req = [NSString stringWithFormat:BaseURLForLocation, esc_addr];
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];

    NSDictionary *googleResponse =
    [NSJSONSerialization JSONObjectWithData: [result dataUsingEncoding:NSUTF8StringEncoding]
                                    options: NSJSONReadingMutableContainers
                                      error: nil];

    NSDictionary *resultsDict = [googleResponse valueForKey:@"results"];
    NSDictionary *geometryDict = [resultsDict valueForKey:@"geometry"];
    NSDictionary *locationDict = [geometryDict valueForKey:@"location"];
    NSArray *latArray = [locationDict valueForKey: @"lat"]; NSString *latString = [latArray lastObject];
    NSArray *lngArray = [locationDict valueForKey: @"lng"]; NSString *lngString = [lngArray lastObject];

    CLLocation *locationFound = [[CLLocation alloc] initWithLatitude:[latString doubleValue] longitude:[lngString doubleValue]];

    NSLog(@"%s lat: %f\tlon:%f ",__FUNCTION__, [latString doubleValue], [lngString doubleValue]);

    return locationFound;
}

Then I used CLGeocoder class

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        for(CLPlacemark *aPlacemark in placemarks){
            NSString *strZipCode = [aPlacemark.addressDictionary objectForKey:(NSString *)kABPersonAddressZIPKey];

        }
    }];

Hope this help..

3
On

After digging into the SDK I found out, that you can access a user's zipcode and street using the FBGraphLocation API, which is part of the FBGraph API.

To get a FBGraphLocation you'll acess the location property of a FBGraphUser

/*!
 @property 
 @abstract Typed access to the user's current city.
 */
@property (retain, nonatomic) id<FBGraphLocation> location;

The FBGraphLocation API has those properties available:

/*!
 @property
 @abstract Typed access to a location's street.
 */
@property (retain, nonatomic) NSString *street;

/*!
 @property
 @abstract Typed access to a location's zip code.
 */
@property (retain, nonatomic) NSString *zip;

That's what you need, right?