Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload photo using new iOS Facebook SDK API (3.0)

How can I upload a photo to facebook from an iOS app using their new API/SDK? I've already tried and I'm not getting anywhere, just keep running in circles. Here is the code I currently have:

-(void)dataForFaceboo{
    self.postParams =
    [[NSMutableDictionary alloc] initWithObjectsAndKeys:
     self.uploadPhoto.image, @"picture", nil];
}

-(void)uploadToFacebook{

    [self dataForFacebook];

    NSLog(@"Going to facebook: %@", self.postParams);

    // Hide keyboard if showing when button clicked
    if ([self.photoCaption isFirstResponder]) {
        [self.photoCaption resignFirstResponder];
    }
    // Add user message parameter if user filled it in
    if (![self.photoCaption.text
        isEqualToString:kPlaceholderPostMessage] &&
        ![self.photoCaption.text isEqualToString:@""]) 
    {
        [self.postParams setObject:self.photoCaption.text forKey:@"message"];
    }
    [FBRequestConnection startWithGraphPath:@"me/feed"
        parameters:self.postParams
        HTTPMethod:@"POST"
        completionHandler:^(FBRequestConnection *connection,
                            id result,
                            NSError *error) 
    {
        NSString *alertText;
        if (error) {
            alertText = [NSString stringWithFormat:
                         @"error: domain = %@, code = %d",
                      error.domain, error.code];
        } else {
            alertText = [NSString stringWithFormat:
                         @"Posted action, id: %@",
                         [result objectForKey:@"id"]];
        }
        // Show the result in an alert
        [[[UIAlertView alloc] initWithTitle:@"Result"
                              message:alertText
                              delegate:self
                              cancelButtonTitle:@"OK!"
                              otherButtonTitles:nil] show];
    }];
}
like image 310
user1195300 Avatar asked Aug 15 '12 18:08

user1195300


1 Answers

Your code is fine, some slight changes to be done:

add the image to the dictionary in NSData format, like

[params setObject:UIImagePNGRepresentation(_image) forKey:@"picture"];

and change the graph path to "me/photos" instead of "me/feed"

Make these changes, it worked for me. Remember you need to use "publish_actions" permissions.

like image 161
vishy Avatar answered Oct 01 '22 04:10

vishy