Search code examples
iosfacebooktwitterios6

Share photo from UIImagePicker to Facebook thru SLComposeView


I was trying to allow the user to pick media from their Camera Roll, then share it to Facebook using the iOS 6 Social framework.

Below is my didFinishPickingMediaWithInfo:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    image1 = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    NSData *chosenImage =UIImageJPEGRepresentation(image1, 1.0);
    imageView.image = [UIImage imageWithData:chosenImage];
    [self dismissViewControllerAnimated:YES completion:nil];
}

And this is what I call when the user taps on a button:

-(IBAction)shareImageToFacebook:(id)sender
{
    //This is to display the UIImagePicker to let the user pick an image
    controller = [[UIImagePickerController alloc]init];
    controller.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    controller.allowsEditing=YES;
    [controller setDelegate:self];
    [self presentViewController:controller animated:YES completion:nil];

    //This is to display the SLComposeView to let the user share the photo
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [fbSheet setInitialText:@"Facebooking from my own app! :)"];

        [fbSheet addImage:imageView.image]; //This is where I try to add my image

        [self presentViewController:fbSheet animated:YES completion:nil];
    }
    else if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        [self dismissViewControllerAnimated:NO completion:nil];
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Sorry"
                                  message:@"You can't post to Facebook right now, make sure your device has an internet connection and you have at least one Facebook account setup"
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

What I was originally trying to do is to first let the user pick the photo from the Camera Roll, then display the Facebook share button to allow the user to share the photo they just picked from the Camera Roll.

Currently, my workaround (which is on the above code) is to set the image the user picked onto an invisible UIImageView, then add the image of the UIImageView to the SLComposeViewController. The 'workaround' does not work, and not only that, the console says that I was attempting to present 2 views (the UIImagePickerController and the SLComposeViewController). The direct warning message from the console is:

Warning: Attempt to present <SLFacebookComposeViewController: 0x94f2580> on <UINavigationController: 0xa4b5240> while a presentation is in progress!

I want to know how do I allow the user to select an image in their Camera Roll, then share the image through the Social framework.


Solution

  • Write faceBook code in -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    {
        image1 = [info objectForKey:UIImagePickerControllerOriginalImage]; 
        NSData *chosenImage =UIImageJPEGRepresentation(image1, 1.0);
        imageView.image = [UIImage imageWithData:chosenImage];
            [self dismissViewControllerAnimated:YES completion:^(void){
        //write code here
    
    
    
       if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
        {
            SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
            [fbSheet setInitialText:@"Facebooking from my own app! :)"];
    
            [fbSheet addImage:imageView.image]; //This is where I try to add my image
    
            [self presentViewController:fbSheet animated:YES completion:nil];
        }
        else if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
        {
            [self dismissViewControllerAnimated:NO completion:nil];
            UIAlertView *alertView = [[UIAlertView alloc]
                                      initWithTitle:@"Sorry"
                                      message:@"You can't post to Facebook right now, make sure your device has an internet connection and you have at least one Facebook account setup"
                                      delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
            [alertView show];
        }
        }];
    }