Marti Serra Vivancos
Marti Serra Vivancos

Reputation: 1189

How can I share a picture with Facebook SDK iOs

I'm developing a iOs 5 app and I've implemented Facebook SDK for iOs. I would like to share a picture so that's my code (isn't working):

if (buttonIndex==3) {


    if (facebook == nil) 
    {
        // Setup Facebook connection
        [self setUpFacebookConnection];
    }else {
        [self fbDidLogin];
    }          
}   

 }
- (void)setUpFacebookConnection
{
facebook = [[Facebook alloc] initWithAppId:@"245697495524266" andDelegate:self];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] 
    && [defaults objectForKey:@"FBExpirationDateKey"]) 
{
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}

if (![facebook isSessionValid])
{
    NSArray* permissions = [[NSArray alloc] initWithObjects:
                            @"offline_access", @"publish_stream", @"publish_actions", @"photo_upload", nil];

    [facebook authorize:permissions];
}
 }

 - (void)fbDidLogin{ 

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
 [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
 [defaults synchronize];


 bannerview2.hidden=YES;

 if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
 else
    UIGraphicsBeginImageContext(self.view.bounds.size);

// retrieve the current graphics context
 CGContextRef context = UIGraphicsGetCurrentContext();

// render view into context
[self.view.layer renderInContext:context];

// create image from context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// save image to photo album
UIImageWriteToSavedPhotosAlbum(image,
                               self,
                               nil,
                               @"image.png"); 
bannerview2.hidden=NO;


NSMutableString *message = [[NSMutableString alloc] init];
[NSString stringWithFormat:@"Resultado final %@: %d vs. %@: %d. Mi jugador hizo %d puntos y cometió %d faltas.",intnomlocal, puntsl, intnomvisitant, puntsv, puntsjugadorint, faltesjugadorint];




 NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image,     @"picture", message,@"message", nil];

  [facebook requestWithGraphPath:@"me/feed"andParams:params andHttpMethod:@"POST" andDelegate:self];    

}

So it all works unless the part of sharing the picture in -(void) fbDidLogin. How can I fix it?

Upvotes: 0

Views: 9166

Answers (2)

Blitz
Blitz

Reputation: 5671

You are missing the necessary permissions. As per https://developers.facebook.com/docs/reference/api/permissions/ you need the permission publish_stream. However, even with that permission, i'm not 100% sure that you're allowed to post to me/photos. Try posting to the feed first!

[edit] turns out I was wrong. To upload an UIImage, you need both the user_photos, publish_stream and upload_photos permission and post to me/photos - you can't post to me/feed with an image to upload.

The newer facebook api explorer, directly from fb, is a bit better. https://developers.facebook.com/tools/explorer

You can check the documentation here under "Publishing" for what and where you can publish. Note that you can only upload photos to existing albums, for which you need the album-ID.

You should implement the FBRequestDelegate protocol in your class to see what's failing and how.

[edit 2] check this post: other SO post - it seems you really need the publish_stream permission as well. Try it again, if you can't get around it, I'll setup a test environment.

Upvotes: 2

Malek_Jundi
Malek_Jundi

Reputation: 6160

try to add the access token in params dic

 NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image,  @"picture",message,@"caption",[facebook accesstoken],@"access_token",nil];

Upvotes: 0

Related Questions