Shubham
Shubham

Reputation: 813

Uploading UImage to facebook

My following code for posting the UIImage to user's facebook wall is not working .I've looked into the sample code that comes along with the facebook sdk and its working fine. I am unable to find whats going wrong with my code.

-(void) shareOnFacebook
{
facebook = [[Facebook alloc] initWithAppId:@"MYAPPID" andDelegate:(id)self];

NSArray * neededPermissions = [[NSArray alloc] initWithObjects:@"offline_access",@"user_about_me", @"publish_stream", @"user_photos", nil] ;
[facebook authorize:neededPermissions ];

if (![facebook isSessionValid]) 
{





    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   img, @"picture",
                                   nil];
    [facebook requestWithGraphPath:@"me/photos"
                                    andParams:params
                                andHttpMethod:@"POST"
                                  andDelegate:(id)self];



} 

}

please tell me that what can be the problems?

thanks in advance..

Upvotes: 0

Views: 376

Answers (2)

Malek_Jundi
Malek_Jundi

Reputation: 6160

you need to call the upload function after the delegate method - (void)fbDidLogin is being called .. try to do it like this and it should work

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[fb accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[fb expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

    [self uploadImageWithName:yourImageName];
}


- (void) uploadImageWithName : (NSString*)name
{
    NSString *img = [NSString stringWithFormat:@"%@.PNG",name];
    UIImage *uploadImage = [UIImage imageNamed:img];

    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   [fb accessToken], 
                                   @"access_token",nil];

    [params setObject:uploadImage forKey:@"source"];

    [fb requestWithGraphPath:@"me/photos"
                             andParams: params
                         andHttpMethod: @"POST"
                           andDelegate:self];

}

Upvotes: 2

zoul
zoul

Reputation: 104115

It does not make sense, does it? You ask the facebook object if the session is valid and if not, you proceed to upload the photo. I don’t know if the rest of the code is fine, but at least try to remove the condition or change it to if ([facebook isSessionValid]) and see if that helps.

Upvotes: 0

Related Questions