FBRequest get parameters issue - iphone

I want to get in my app info about the user. His name, age, location, gender, profile image and other stuff.
For now i am using :
-(void)facebookOpenSession{
FBRequest *me = [FBRequest requestForMe];
[me startWithCompletionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSDictionary *resultDic = (NSDictionary<FBGraphUser> *) result;
facebookData = [[NSMutableDictionary alloc]initWithDictionary:resultDic];
FBRequest *pic = [FBRequest requestForGraphPath:#"me/?fields=picture"];
[pic startWithCompletionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSDictionary *resultDic = (NSDictionary<FBGraphUser> *) result;
NSDictionary *dic = [resultDic objectForKey:#"picture"];
NSDictionary *dic2 = [dic objectForKey:#"data"];
NSString *imgUrl = [dic2 objectForKey:#"url"];
NSLog(imgUrl);
}];
}];
}
I made two requests ine for the user profile and one other for the image, and i want to know if i can make only one call? and if i want to get another info what i need to do?

The way you annotated your code is a little weird to read for me, so I re-wrote it like so:
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
self.nameString = user.name;
self.profileImage.profileID = user.id;
self.userName = user.username;
}
}];
}
It really isn't much more complicated than how I wrote it.
Happy Coding!
Addition to code due to comments:
You need to get the output from Facebook. For an image, your code should look like:
FBProfilePictureView *profileImage.profileID = user.id;
user.id comes from Facebook once you initiate an FBRequest.
You don't really convert the FBProfilePictureView into a UIImageView, but instead, set the FBProfilePictureView as a subview of a UIView that you create.
Also, you need to set the picture cropping:
profileImage.pictureCropping = FBProfilePictureCroppingSquare;
FBProfilePictureCroppingSquare is one of a few choices that you have. Read up on the documentation in Facebook Developer to see your options.

Related

Post on Facebook Page from iOS App Not Showing Up on Page?

I am posting a picture and dummy text on a facebook page with the Page Id "848190045198980".I am signing in using my facebook credentials.Also I have admin role for the specific page.So I am just getting the basic permissions from facebook.Below is the request and the response.
Request :
UIImage * productImage = [UIImage imageNamed:#"corporate.png"];
NSData * imageData = UIImagePNGRepresentation(productImage);
NSDictionary *params = #{#"message": #"Hello XYZ", #"source":imageData};
[FBRequestConnection startWithGraphPath:#"/848190045198980/photos" parameters:params HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error)
{
if(completionBlock)
{
NSLog(#"%#",result);
completionBlock(YES,result,nil);
}
}
else
{
if(completionBlock)
completionBlock(NO,nil,error);
}
}];
Response :
{"id":"1481175438805031","post_id":"848190045198980_1481175438805031"}
Expected Result : My Post should show up on the Facebook Page in "Posts to Page" Section.
Actual Result : Nothing Shows Up.
Please point me in the right direction as to what I am missing here.I have checked the API Doc several times and I am finding it real tough as to where I am missing something.
Regards,
Roman
This is how I am posting video on Facebook:
NSDictionary *params = #{
#"message":message,
#"tags":Mystring,
#"link":#"http://www.miimobileapp.com/website/index.html",
#"privacy": #"{'value': 'SELF'}",
};
/* make the API call */
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/feed"
parameters:params
HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,id result,NSError *error) {
NSLog(#"Post successful");
}

What is correct iOS syntax to create a Facebook event then send an invite to a user?

I have a session type called FBSession. I know the session is open, but how do I go about sending a graph request to create an event, then invite a user to that event? I know the users' Facebook IDs, as I managed to pull them from the local addressbook properties.
if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
// even though we had a cached token, we need to login to make the session usable
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
switch (status) {
case FBSessionStateOpen:
{
/* What do I do here to create the event and invite IDs x, y and z where z, y and z are long long Facebook Ids */
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"venue name",#"name",
#"2012-01-13T17:00:00+0000",#"start_time",
#"2012-01-16T01:30:00+0000",#"end_time",#"location",#"location name ch",#"1234567890",#"id", nil];
FBRequest *facebook = [[FBRequest alloc] initWithSession:appDelegate.session graphPath:#"me/events" parameters:params HTTPMethod:#"POST"];
Is any of this correct, and which delegate methods am I required to run to check if it worked OK back in the appDelegate?
OK did this seems cool.
FBRequest *eventPostOK = [FBRequest requestWithGraphPath:#"me/permissions" parameters:Nil HTTPMethod:GET];
[eventPostOK startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
BOOL canDoIt = FALSE;
if (!error)
{
FBGraphObject *data = [result objectForKey:#"data"];
for(NSDictionary<FBGraphObject> *aKey in data) {
canDoIt = [[aKey objectForKey:#"create_event"] boolValue];
}
}
if (canDoIt) { ... DO STUFF .... }
];

FQL query with v3.1 Facebook SDK for iOS to get birthday and email

Can anyone help me. I cannot figure out how to make a single FQL query using the latest Facebook SDK (v 3.1) for iOS to get birthday and email of user's friend. When I query for fields like name i get the correct value but get null for email and birthday field.
Here is my code
- (void)facebookViewControllerDoneWasPressed:(id)sender {
// we pick up the users from the selection, and create a string that we use to update the text view
// at the bottom of the display; note that self.selection is a property inherited from our base class
for (id<FBGraphUser> user in _friendPickerController.selection) {
_nameTxt.text = user.name;
NSString *fql = [NSString stringWithFormat:#"SELECT email, birthday, name FROM user WHERE uid = %# ",user.id];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:fql forKey:#"q"];
[FBRequestConnection startWithGraphPath:#"/fql"
parameters:params
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(#"Error: %#", [error localizedDescription]);
} else {
NSLog(#"Result: %#", result);
}
}];
}
[self.navigationController dismissModalViewControllerAnimated:YES];
}
I'm getting value for name but null for email and birthday.
Thanks
Before you call your query, you'll need to have the user log in and ask for email and user_birthday permissions. For example:
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"email",
#"user_birthday",
nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
}
The above method is used in the context of this tutorial:
https://developers.facebook.com/docs/howtos/login-with-facebook-using-ios-sdk/
Also check out the FQL tutorial:
https://developers.facebook.com/docs/howtos/run-fql-queries-ios-sdk/

How to check if a user has granted a certain permission Facebook iOS

In my iOS app, I just want to check if the user has granted the Facebook publish_stream permission.
I'm not sure how to handle the response to the call
[facebook requestWithGraphPath:#"me/permissions" andDelegate:self];
in my FBRequest delegate method. I've tried:
if (request == self.permissionRequest) {
NSString *key = [result objectForKey:#"publish_stream"];
DLog(#"Key: %#", key);
}
But I get null.
And if I try
id *key = [result objectForKey:#"publish_stream"];
int keyInt = [key integerValue];
DLog(#"Key: %i", keyInt);
I always get 0. Even when I know the permission is active...
You can make an FBRequest with the open graph API with "me/permissions".
It will return you a response with a dictionary where the key are the permissions. a value will be associated (1 = YES, 0 = NO)
Here's a working sample I did for create_events. Sure it could be used for others:
FBRequest *eventPostOK = [FBRequest requestWithGraphPath:#"me/permissions" parameters:Nil HTTPMethod:#"GET"];
[eventPostOK startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
BOOL canDoIt = FALSE;
if (!error)
{
FBGraphObject *data = [result objectForKey:#"data"];
for(NSDictionary<FBGraphObject> *aKey in data) {
canDoIt = [[aKey objectForKey:#"create_event"] boolValue];
}
}
else
NSLog(#"%#", error);
NSLog(#"%#", canDoIt ? #"I can create Events" : #"I can't create Events");
}];

Facebook Graph API for iOS search

I am trying to search places from the GraphAPI using following code without luck. Can anybody please enlight my path ?
If I try to post link/message/photo it works as expected but when trying to get location it always fails and gives me **The operation couldn’t be completed. (facebookErrDomain error 10000.)**
//Following statement is using permissions
NSArray * permissions = [NSArray arrayWithObjects:#"publish_stream",#"user_checkins", #"friends_checkins", #"publish_checkins", nil];
[facebook authorize:FB_APP_ID permissions:permissions delegate:_delegate];
NSString *centerString = [NSString stringWithFormat: #"%f,%f", 37.76,-122.427];
NSString *graphPath = #"search";
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"coffee",#"q",
#"place",#"type",
centerString,#"center",
#"1000",#"distance", // In Meters (1000m = 0.62mi)
nil];
[facebook requestWithGraphPath:_path andParams:_params andHttpMethod:#"POST" andDelegate:_delegate];
Never mind. Downloaded latest sample HackBook from facebook for graph api from github and it includes sample code for the same.
For "search" you should use "GET" instead of "POST".
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#search
With Facebook iOS SDK, you can use FBRequestConnection after login.
[FBRequestConnection startWithGraphPath:#"search?q=coffee&type=place&center=37.76,-122.427&distance=1000"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Sucess! Include your code to handle the results here
NSLog(#"result: %#", result);
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"error: %#", error);
}
}];
With Last SDK
NSMutableDictionary *params2 = [NSMutableDictionary dictionaryWithCapacity:3L];
[params2 setObject:#"37.416382,-122.152659" forKey:#"center"];
[params2 setObject:#"place" forKey:#"type"];
[params2 setObject:#"1000" forKey:#"distance"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"/search" parameters:params2 HTTPMethod:#"GET"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"RESPONSE!!! /search");
NSLog(#"result %#",result);
NSLog(#"error %#",error);
}];