I am using Facebook SDK in my iPhone app. I want to find the user's Facebook profile photos, for that I am using the below code:
-(IBAction)FacebookLogin:(id)sender{
if (FBSession.activeSession.isOpen) {
[self findAlbums];
} else {
NSArray *permissions = [[NSArray alloc] initWithObjects:#"user_photos",
nil];
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// if login fails for any reason, we alert
if (error) {
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
[self findAlbums];
}
}];
}
}];
}
}
-(void)findAlbums {
[FBRequestConnection startWithGraphPath:#"/me/albums"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
NSLog(#"result::%#",result);
}];
}
Out put ---- data = ( );
This does not give any albums in data. The Facebook user logged in have many albums and photos in his profile. Why this happens?
just change the code in find albums to :
[FBRequestConnection startWithGraphPath:#"me/albums"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Success! Include your code to handle the results here
NSLog(#"user events: %#", result);
NSArray *feed =[result objectForKey:#"data"];
for (NSDictionary *dict in feed) {
NSLog(#"first %#",dict);
}
} else {
// An error occurred, we need to handle the error
// Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
NSLog(#"error %#", error.description);
}
}];
you have to add the Facebook credentials(i.e facebook account) you're using to the Facebook developer account settings(i.e as admin or developer) , only then you can access the album's photos through your code
Related
with this bloc of code, I get all the albums of a user, but I don't have the photo where the user is "tag". How I can get this pictures like any other album of the user.
[FBSession openActiveSessionWithPublishPermissions: [NSArray arrayWithObjects: #"user_photos", nil] defaultAudience: FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler: ^(FBSession *session,FBSessionState status,NSError *error)
{
if (error)
{
NSLog(#"error");
return;
}
else
{
[FBSession setActiveSession:session];
NSLog(#"LOGIN SUCCESS");
[FBRequestConnection startWithGraphPath:#"me/albums?fields=id,name,count"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection, id result, NSError *error)
{
NSLog(#"%#",result);
self.arrayAlbumDetails = result[#"data"];
if (self.arrayAlbumDetails.count == 0)
{
NSLog(#"Either the user does not have any albums or something bad happen.");
}
else
{
[self.tblFacebookAlbumList reloadData];
}
}];
}
}];
Thanks
I need to make a post on a my own fanpage as the admin.
I created this code hopping that passing the id of the page "fbPageID" would leave the post on that page, but it actually leaves the post on my profile.
NSArray *publishPerms = #[#"manage_pages",#"publish_actions", #"publish_stream"];
[FBSession openActiveSessionWithPublishPermissions:publishPerms defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:NO completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (session.isOpen) {
[FBRequestConnection startForPostStatusUpdate:self.textBox.text place:fbPageID tags:nil completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error){
NSLog(#"error %#",error);
}
else{
NSLog(#"POSTED %#",result);
}
}];
}
else{
NSLog(#"session not open");
}
This is helpfull for Post on user Facebook..
Step 1 - Import FBSDK in your project
Step 2 - Login Authentication
NSDictionary *params = #{
#"message": #"This is a test message",
};
/* make the API call */
FBSDKGraphRequest *request_ = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/feed"
parameters:params
HTTPMethod:#"POST"];
[request_ startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
// Handle the result
}];
This is the most easy and simple code I have used to upload images to Facebook it works perfectly on my simulator and on my device in testing phase. But when I published my app to iTunes it 80% got crash and 20% made success to upload image to facebook. Why this is happening ?
Xcode shows warning that "openActiveSessionWithPermissions is deprecated"
if (FBSession.activeSession.isOpen)
{
[self UploadToFb];
}
else // Take permissions
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_stream",
nil];
[self controlStatusUsable:NO];
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// if login fails for any reason
if (error) {
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
[self UploadToFb];
}
}];
}
}];
}
After taking Permissions from user Upload image to Facebook
-(void)UploadToFb
{
[FBRequestConnection startForUploadPhoto:myImg.image
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error)
{
[FBRequestConnection startForUploadPhoto:myImg.image
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"Uploaded");
}
else
{
NSLog(#"Error");
}
}];
}
else
{
NSLog(#"Error");
}
}];
}
I am using facebook SDK for iOS in my app.
-(IBAction)FacebookLogin:(id)sender{
if (FBSession.activeSession.isOpen) {
[self promptUserWithAccountNameForStatusUpdate];
}
else {
NSArray *permissions = [[NSArray alloc] initWithObjects: #"publish_stream",#"email",nil];
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
if (error) {
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
[self promptUserWithAccountNameForStatusUpdate];
}
}];
}
}];
}
}
-(void)promptUserWithAccountNameForStatusUpdate {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
}
}];
}
How can I fetch profile photos of user ?
I want to fetch his previous 4 profile photos also.
Just like in the app Tinder.
How to do this ?
Ok..I found that
We will get all albums with the below url
https://graph.facebook.com/[uid]/albums?access_token=[AUTH_TOKEN]
Then we can pass album ID to below url
https://graph.facebook.com/[ALBUM_ID]/photos?access_token=[AUTH_TOKEN]
We will get all profile images...bingo :) :) ;)
I am tried to retrieve the Email Address from Facebook Friends.but,It cannot retrieved the email address.
I printed the email address.but,It displays the null.
I added email address in permissions array,FBRequest.
-(void)LoginButtonTapped
{
[FBSession.activeSession openWithCompletionHandler:^(FBSession *session,
FBSessionState state,
NSError *error)
{
if (FBSession.activeSession.isOpen)
{
NSLog(#"TOKEN : %#",[[FBSession activeSession]accessTokenData]);
FBRequest *friendRequest = [FBRequest requestForGraphPath:#"me/friends?fields=name,picture,birthday,email,location"];
[friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSArray *data = [result objectForKey:#"data"];
for (FBGraphObject<FBGraphUser> *friend in data)
{
[delegate.friendsListArray addObject:friend];
NSLog(#"%#:%#:%#:", [friend name],[friend birthday],[friend id]);
NSString *email=[friend objectForKey:#"email"];
NSLog(#"Email:%#",email);
}
}];
}
}];
}
- (void) performPublishAction:(void (^)(void)) action
{
NSArray *permissions = [[NSArray alloc] initWithObjects:#"user_birthday",#"user_location",#"friends_email",#"friends_hometown",#"friends_birthday",#"friends_location",#"publish_actions",#"user_photos",nil];
[[delegate facebook]authorize:permissions];
if ([FBSession.activeSession.permissions indexOfObject:permissions] == NSNotFound)
{
[FBSession.activeSession requestNewPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
action();
}
}];
}
else
{
action();
}
[permissions release];
}
Any ideas please help me.
Shot in the dark but did you check your error value? Also what's the count of the # of records you get back?
Also could just try this instead, which seems to work (you might need to use objectForKey to get email out of the object):
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"I have a friend named %# with id %#", friend.name, friend.id);
}
}];