Retrieve country from Facebook SDK - IOS - ios

I'm using Facebook SDK to get user's details with permissions:
[NSArray arrayWithObjects:#"user_photos",#"public_profile",#"user_location", #"user_birthday",nil];
I know how to get the city but how can I get user's country?
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
NSDictionary *userData = (NSDictionary *)result;
NSString *facebookID = userData[#"id"];
NSString *name = userData[#"name"];
NSString *city = userData[#"location"][#"name"];
}
else
{
//...
}
}];

It should be [#"location"][#"country"] as #trick14 outlined. If you can't get a value from it, it can have multiple reasons:
The user you're querying has no location information in his profile
The Access Token you're using has no granted user_location permission (Check with a call to /me/permissions)
There's no country info for the location (quite unlikely)
You have a problem with your permissions string BTW, user_location is contained twice, and offline_access and publish_stream are deprecated long ago.

try this:
NSString *city = userData[#"location"][#"city"];
See here for more info.. :)

Related

schedule post on facebook page using facebooksdk

I have written following code to post on facebook page but getting this error message: “(#200) Unpublished posts must be posted to a page as the page itself.“;
NSString *accesstoken = #"EAACEdEose0cBAAe49xNZBS5MqswXkRptXgLDRuZBZBPZCZCVl5rxW6Bt0jthaHXMACNTiUWePg9XkVTpttDNsLyWYBaEkQaUCm5vbSJQh8zGwkegAcQRjRggAJajunmMyg0jVIKZAZBzMjbZCKWUv1Tie1UzF8gJCpIxACIVgqx7SqKdPZBnIUaElk3meB7SYo6AZD";
NSString *pageId = [dic valueForKey:#"id"];
NSDictionary *dicPara=#{#"message": #"Hello",#"access_token":accesstoken,#"scheduled_publish_time": [NSNumber numberWithInt:timestamp],#"published": #"false"};
NSLog(#"%#",dicPara);
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:#"/%#/feed",pageId]
parameters:dicPara
HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Insert your code here
if (error)
{
NSLog(#"%#",error);
}
else
{
[arrPostId addObject:[result valueForKey:#"id"]];
}
}];
Now I have searched for this error and found that you have to set accesstoken of the page instead of user's token. So how can I change user's access token to page accesstoken?

Post a photo on a Facebook Public page

I'm trying to post a photo from an iOS app to a public page.
I log in the app as the Admin of the FB App and when I got the activity token through Graph API:
FBSDKGraphRequest *request2 = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/accounts"
parameters: #{#"access_token": item.token}
HTTPMethod:#"GET"];
[request2 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error)
{
NSDictionary *userData = (NSDictionary *)result;
NSArray* arraydata = [userData objectForKey:#"data"];
NSDictionary *dataD = [arraydata objectAtIndex:0];
NSString *facebookID = dataD[#"id"];
token= dataD[#"access_token"];
}
I get the page token that I need to post on the page, but even if in the json I see I have all the permissions, when I post the photo it returns a 200 error (additional permission required).
If I try to post on the Admin profile (not the public page), everything works.
Any idea? What I'm missing? Can anyone you please solve this issue?

Get Facebook Friends returns empty

I am trying to get a list of friends for my app, however this is what the console returns:
{
data = (
);
summary = {
"total_count" = 1221;
};
The total count is right but it doesn't show me any of my friends?
I have created two test users on the facebook developer portal and they have both logged in with my app, and they are friends with me on facebook, shouldn't their data be showing?
Here is my code for requesting the permissions and such, this class is called each time the user logs in.
#implementation UploadUser
NSString *userName;
NSString *userId;
NSString *userGender;
//UIImage *userPicture;
NSString *userPicture;
NSMutableURLRequest *urlRequest;
//NSData *imageData;
//NSData *pictureData;
//NSData *tempData;
//Retrieves users name, gender and id
-(void)getUserInfo {
FBSDKGraphRequest *requestUser = [[FBSDKGraphRequest alloc]initWithGraphPath:#"me" parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestUser completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
userName = result[#"name"];
userGender = result[#"gender"];
userId = result[#"id"];
if (connection) {
if (!error) {
[self setUserData];
[self uploadUser];
}
}else if (!connection) {
NSLog(#"Get User %#",error);
}
}];
[connection start];
//Get Friends
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/friends"
parameters:nil
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (connection) {
NSLog(#"Friends = %#",result);
}else if (!connection) {
NSLog(#"Get Friends Error");
}
}];
}
//Saves user data to class
-(void)setUserData {
DataManager *dm = [DataManager sharedInstance];
[dm setObject:userName forKey:#"userName"];
[dm setObject:userGender forKey:#"userGender"];
[dm setObject:userId forKey:#"userId"];
NSLog(#"%#",userId);
NSLog(#"%#",userName);
NSLog(#"%#",userGender);
}
//Uploads user data online
-(void)uploadUser {
PFUser *user = [PFUser currentUser];
user[#"name"] = userName;
user[#"id"] = userId;
user[#"gender"] = userGender;
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(#"User Data Uploaded :)");
} else {
//Handle Error
NSLog(#"User Upload Error... %#",error);
}
}];
}
Why are isn't my query returning my friends? Is it because my friends are not authorised to use my app? Is my permissions not being asked properly?
Also you can use Graph API Explorer in order to quickly check if your request works:
https://developers.facebook.com/tools/explorer/
If you want to check you friends you should ask your friend also use this link and get token. In this way the facebook system understand that your friend login in Graph API app and you will get the list of friends.
Since v2.0 of the Graph API, you can only get the friends who authorized your App with user_friends too. This is subject of countless Stackoverflow threads already, for example: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app
Check this one Get Facebook friends

Unable to get users data from facebook using graph API

I am making in app in which i show user's hometown,location on profile screen. I get the required data when using developer's account and get data for every user when current user is looking at its own profile.
But when i try to look at other's profile, i don't get the desired data. Here is my code for how i am getting the data out.
-(void)loadFbData{
NSMutableString *graphPath = [NSMutableString stringWithFormat:#"/?id=%#",[self.user valueForKey:#"facebookId"]];
[graphPath appendString:#"&fields=location,hometown,picture.width(640).length(342),name,gender,email,website"];
[FBRequestConnection startWithGraphPath:graphPath completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
dispatch_async(dispatch_get_main_queue(), ^{
self.personalDataView.webLabel.text = [result objectForKey:#"website"];
self.personalDataView.emailLabel.text = [result objectForKey:#"email"];
self.personalDataView.webLabel.text = [result valueForKey:#"website"];
self.profileBasicDataView.livesInLbl.text = [[result objectForKey:#"location"] objectForKey:#"name"];
self.profileBasicDataView.fromLbl.text = [[result objectForKey:#"hometown"] objectForKey:#"name"];
});
NSURL *url = [NSURL URLWithString:[[result objectForKey:#"picture"] valueForKeyPath:#"data.url"]];
[self.profileBasicDataView loadUserFbData:result];
[self.profileImage loadImage:url];
}
}];
}
**website and email is hidden when other user's profile is shown.

get user email from Facebook Using FBgraphUser In Parse

Im Trying to store the users email address in mt parse database but i cant seem to find the right way of getting this done
my code below
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *result, NSError *error) {
if (error) {
// Handle it
} else {
PFUser *me = [PFUser currentUser];
me[#"facebookId"] = result.objectID;
me[#"facebookName"] = result.name;
me[#"facebookProfile"] = result.link;
[me saveEventually];
}
}];
}
Thanks in advance
Actually, result is an NSDictionary instance, conforming to FBGraphUser protocol. That mean it still can be treated as dictionary, for example you can send -objectForKey: message to it. Actually FBGraphUser protocol only adds a "shortcuts" for some objects, stored in the dictionary. So, by calling:
NSString *mail = result[#"email"];
You will get the email. This is a handy equivalent for:
NSString *mail = [result objectForKey:#"email"];
For all possible keys for user look Graph Api Reference.

Resources