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 :) :) ;)
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 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
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");
}
}];
}
a question about Facebook sdk for iOS.
If I have an active session how can I get only the user ID?
Is there a simple way to obtain this id without use this example code?
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *aUser, NSError *error) {
if (!error) {
// I can take the user id..
}
}];
}
+ (NSString *)activeUserId {
return [FBSDKAccessToken currentAccessToken].userID;
}
if you are already logged in on facebook.
This is best way to take user id, instead of directly access using dot.
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *aUser, NSError *error) {
if (!error) {
NSLog(#"User id %#",[aUser objectForKey:#"id"]);
}
}];
[FBRequestConnection startWithGraphPath:#"/me"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
NSDictionary *result,
NSError *error
) {
/* handle the result */
_fbId = [result objectForKey:#"id"];
}];
I am running into issues trying upgrade my Facebook SDK to the latest production release (FacebookSDK-3.0.8.pkg - Facebook SDK 3.0 for iOS (update 1) [August 21, 2012]).
I am following along with tutorial on this page.
I have ran into several issues trying to get the code to work, it's not as easy as it proclaims to be in the tutorial. I can get my session open, but can not get the request to work.
- (IBAction)facebookTapped:(id)sender {
[FBSession openActiveSessionWithPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if(error) {
NSLog(#"Error opening session: %#", error);
return;
}
if(session.isOpen) {
NSLog(#"session is open");
FBRequest *me = [FBRequest requestForGraphPath:#"me"];
[me startWithCompletionHandler:^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *my,
NSError *error) {
NSLog(#"My name: %#", my.first_name);
}];
}
}];
}
My console displays that the session is open if I remove the call to FBRequest requestforGraphpath. If I leave it in, I receive the error "Incompatible block pointer types initializing 'void(^)(struct FBRequestConection , struct NSDictionary, struct NSError*)', expected 'FBRequestHandler'
Now what has me stumped is that this is the exact code shown in the tutorial, excpet that I changed out [FBRequest requestForMe] trying different approaches. None worked.
Can anyone shed some light on this for me?
Thank you.
I was able to solve this issue by changing their original block in the tutorial of:
if (session.isOpen) {
FBRequest *me = [FBRequest requestForMe];
[me startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *my,
NSError *error) {
self.label.text = my.first_name;
}];
}
to
if(session.isOpen) {
FBRequest *me = [FBRequest requestForMe];
[me startWithCompletionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSDictionary<FBGraphUser> *my = (NSDictionary<FBGraphUser> *) result;
NSLog(#"My dictionary: %#", my.first_name);
}];
}