Facebook Graph API - how to get user country?

后端 未结 10 1651
心在旅途
心在旅途 2020-11-30 02:56

I\'m developing a Facebook application using their Graph API. I have previously developed an app using the deprecated REST API.

I\'ve come across a problem in that

10条回答
  •  醉话见心
    2020-11-30 03:22

    You should try like this:

    /* make the API call */
    [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection,
                                                               NSDictionary *user,
                                                               NSError *error)
        {
            if (!error)
            {
                self.countryName    = [[[user objectForKey:@"location"] valueForKey:@"name"] componentsSeparatedByString:@", "][1];
                self.countryCode    = [self getCountryCodeForCountry:STUser.countryName];
            }
    }];
    
    -(NSString *)getCountryCodeForCountry:(NSString*)country
    {
        NSMutableArray *countries = [self getCountries];
        NSArray *countryCodes = [NSLocale ISOCountryCodes];
    
        NSDictionary *codeForCountryDictionary = [[NSDictionary alloc] initWithObjects:countryCodes forKeys:countries];
    
        return [codeForCountryDictionary objectForKey:country];
    }
    
    +(NSMutableArray *)getCountries
    {
        NSMutableArray *countries = [NSMutableArray array];
    
        NSArray *countryCodes = [NSLocale ISOCountryCodes];
        countries = [NSMutableArray arrayWithCapacity:[countryCodes count]];
    
        for (NSString *countryCode in countryCodes)
        {
            NSString *identifier = [NSLocale localeIdentifierFromComponents:[NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
            NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
            [countries addObject: country];
        }
    
        return countries;
    }
    

提交回复
热议问题