Fetching Facebook user data using fbsdk 4.2 - ios

I am a beginner in iOS development. I searched a lot but didn't get my code working cause of some deprecated class in latest fbsdk 4.2. What i want is that integration of fb in my apps. I am Able to integrate fb in my app and i can successfully login and logout. Now my app need is to fetch the logged-in user e-mail id and display it in a label. Here is my code, which i have tried.
-(void)viewDidLoad {
[super viewDidLoad];
if ([FBSDKAccessToken currentAccessToken]) {
self.loginButton.readPermissions = #[#"public_profile", #"email", #"user_friends"];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me"
parameters:#{#"fields":#"email"}
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if(!error)
{
self.lblEmail.text = [result objectForKey:#"email"];
NSLog(#"email:%#", result);
}
}];
}

this is working fine with me for facebook sdk 4.2
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil] startWithCompletionHandler: ^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
NSLog(#"%#",result);
else
NSLog(#"%#",error);
}];
}
Hope this will help you.

Related

Unable to post status update from ios app

I am trying to post a status on my account. I have been trying since couple of hours but can't get it working.
- (void) shareStatusUpdateButtonClicked {
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me/feed" parameters: #{ #"message" : #"hello world"}HTTPMethod:#"POST"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"POST ID: %#",result);
}
}];
}
else {
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithPublishPermissions:#[#"publish_actions"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (!error) {
NSLog(#"Permission Granted");
NSDictionary *params = #{#"message": #"This is a test message"};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me/feed" parameters:params HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,id result,NSError *error) {
if(!error){
NSLog(#"RESULT %#",result);
NSLog(#"POSTED !");
}
}];
}
else{
NSLog(#"ERROR PERMISSION NOT GRANTED");
}
}
];
}
}
The problem is when i run the app, it checks if the user has permitted 'publish_actions' in the if expression, it fails then the else block executes, however it displays you have already authorised this app. Then i click Ok but the status is not posted on my facebook profile. I don't understand where i am going wrong. Any help would be apperiated.
PS: I have not asked the user to grant 'publish_actions' permission anywhere else in the code. Also i tried removing the app from facebook setting but it did't help.

Fb login email field not getting in API V. 2.4 [duplicate]

I am currently working on Facebook SDK 4.4.0. Before 4.4.0 version, in 4.3.0 we were getting email, public_profile with the following code
NSArray *arrFBPermission = [[NSArray alloc]initWithObjects:#"email",#"public_profile", nil];
[loginUsingFB logInWithReadPermissions:arrFBPermission handler:^(FBSDKLoginManagerLoginResult *result, NSError *error){
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error){
if (!error){
NSString *fnm = [result valueForKey:#"first_name"];
NSString *lnm = [result valueForKey:#"last_name"];
}
else{
Nslog("%#", error localizedDescription);
}
}];
}
Now in the New SDK (i.e. 4.4.0) I am not getting these values. Why? I want email, first_name, last_name, id from Facebook.
With Facebook 4.4 SDK there is a slight change.
You must have to request the parameter with the FBSDKGraphRequest which you want from Facebook account.
In your code there is nil in parameters :
Update your code with the parameters like as :
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"}]
If you want to login with Facebook with the use of Custom Button then you can use the complete code as follows :
- (IBAction)btnFacebookPressed:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior = FBSDKLoginBehaviorBrowser;
[login logInWithReadPermissions:#[#"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
}
else if (result.isCancelled)
{
// Handle cancellations
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
NSLog(#"result is:%#",result);
[self fetchUserInfo];
[login logOut]; // Only If you don't want to save the session for current app
}
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(#"Token is available : %#",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"resultis:%#",result);
}
else
{
NSLog(#"Error %#",error);
}
}];
}
}
I hope it will work for you.

Not getting Email and public profile using Facebook 4.4.0 SDK

I am currently working on Facebook SDK 4.4.0. Before 4.4.0 version, in 4.3.0 we were getting email, public_profile with the following code
NSArray *arrFBPermission = [[NSArray alloc]initWithObjects:#"email",#"public_profile", nil];
[loginUsingFB logInWithReadPermissions:arrFBPermission handler:^(FBSDKLoginManagerLoginResult *result, NSError *error){
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error){
if (!error){
NSString *fnm = [result valueForKey:#"first_name"];
NSString *lnm = [result valueForKey:#"last_name"];
}
else{
Nslog("%#", error localizedDescription);
}
}];
}
Now in the New SDK (i.e. 4.4.0) I am not getting these values. Why? I want email, first_name, last_name, id from Facebook.
With Facebook 4.4 SDK there is a slight change.
You must have to request the parameter with the FBSDKGraphRequest which you want from Facebook account.
In your code there is nil in parameters :
Update your code with the parameters like as :
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"}]
If you want to login with Facebook with the use of Custom Button then you can use the complete code as follows :
- (IBAction)btnFacebookPressed:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior = FBSDKLoginBehaviorBrowser;
[login logInWithReadPermissions:#[#"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
}
else if (result.isCancelled)
{
// Handle cancellations
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
NSLog(#"result is:%#",result);
[self fetchUserInfo];
[login logOut]; // Only If you don't want to save the session for current app
}
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(#"Token is available : %#",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"resultis:%#",result);
}
else
{
NSLog(#"Error %#",error);
}
}];
}
}
I hope it will work for you.

Facebook iOS SDK v4.0, get friends list

Seems like Facebook iOS SDK v4.0 has a lot of changes. I try to get a user's friends list after he login.
I think it's something to do with FBSDKGraphRequestConnection but I'm not sure how it can be used.
Also, if I only want to get friends who are using my app, does facebook sdk support that?
Can anyone give some examples? Thanks!
FBSDKGraphRequest *friendsRequest =
[[FBSDKGraphRequest alloc] initWithGraphPath:#"me/friends"
parameters:parameters];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:friendsRequest
completionHandler:^(FBSDKGraphRequestConnection *innerConnection, NSDictionary *result, NSError *error) {
...
}];
// start the actual request
[connection start];
This shows how to handle the response:
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me/friends" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
NSArray * friendList = [result objectForKey:#"data"];
for (NSDictionary * friend in friendList)
{
NSLog(#"Friend id: %#", friend[#"id"]);
NSLog(#"Friend name: %#", friend[#"name"]);
}
}
}];
I used below code to get friend list
self.graphrequest = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me/taggable_friends?limit=100"
parameters:#{ #"fields":#"id,name,picture.width(100).height(100)"}];
[self.graphrequest startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
self.resultsArray = result[#"data"];
[self.fbTableView reloadData];
[self.fbTableView setHidden:NO];
} else {
NSLog(#"Picker loading error:%#",error.userInfo[FBSDKErrorLocalizedDescriptionKey]);
}
}];
You can limit the number of friends using limit keyword.Ex: limit=100

ios facebook sdk get and display username

I have been working with the facebook sdk and parse and I am trying to retrieve a users facebook name but the documentation doesn't give me sufficient help and most tutorials on the net are out dated.
Can anyone tell how I can get a users name from the facebook sdk to display as a text label ? This would be greatly appreciated thanks.
You need to implement following FBLoginViewDelegate
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
NSLog(#"user is %# %#", [user objectForKey:#"email"], user.name);
}
You can retrieve it in this way:
FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:#"me"];
[request startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary < FBGraphUser > *my,
NSError *error)
{
NSString *username = my[#"username"];
}
I finally figured it out, you have to request the current users profile information than set your label with a "key" which in this case is called "name"
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me"
parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"fetched user:%#", result);
_usernameText.text = result[#"name"];
}];

Resources