Search code examples
iphonefacebookimageios6slcomposeviewcontroller

Unable to post image into facebook using SLComposeViewController?


I would like to post image into facebook and twitter. I am fine with twitter but not with facebook using SLComposeViewController class. With out add image i am able to post text and url into facebook. The problem is when i use add image i was unable to post this image and also text, url. SLComposeViewController shows image, text and url when i send. I have correct appId and i did not get any errors. But the problem is still there. I don't where the problem is. Please help me.

    - (void)performFBRequestUploadForImage{

        [self showListOfFaceBookAccountsFromStore];
                if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
            {
                SLComposeViewController *mySLComposerSheet = [SLComposeViewController         composeViewControllerForServiceType:SLServiceTypeFacebook];

                 SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){

                     NSString *output;
                    switch (result) {
                        case SLComposeViewControllerResultCancelled:
                            output = @"ACtionCancelled";
                            break;
                        case SLComposeViewControllerResultDone:
                            output = @"Post Successfull";
                            [self dismissViewControllerAnimated:YES completion:nil];
                            break;
                        default:
                            break;
                    }
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Face Book Message" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                    [alert show];
                 };

                [mySLComposerSheet addImage:[UIImage imageNamed:@"images4.jpg"]];
                [mySLComposerSheet setInitialText:@"I am developer."];
                [mySLComposerSheet addURL:[NSURL URLWithString:@"http://stackoverflow.com/"]];
                [mySLComposerSheet setCompletionHandler:completionHandler];
                [self presentViewController:mySLComposerSheet animated:YES completion:nil];
            }

    }

- (void)showListOfFaceBookAccountsFromStore
{
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];


    if( [SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook] )
    {

        NSDictionary *options = @{
        @"ACFacebookAppIdKey" : myAppId,
        @"ACFacebookPermissionsKey" : @[@"publish_stream"],
        @"ACFacebookAudienceKey" : ACFacebookAudienceFriends};
        [accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error){
            if(granted) {
                ACAccount * account = [[accountStore accountsWithAccountType:accountType] lastObject];
                NSLog(@"Facebook user: %@",[account username]);
                if([account username]==NULL){
                    [self facebookAlert];
                } else {



                }

            }
            else{
                  NSLog(@"Read permission error: %@", [error localizedDescription]);
            }
        }];
    } else {
        [self facebookAlert];
    }
}

Solution

  • I used below code in my projects to post the image it works fine.

        if([SLComposeViewController instanceMethodForSelector:@selector(isAvailableForServiceType)] != nil)
        {
            if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
            {
    
                SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    
                SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
                    if (result == SLComposeViewControllerResultCancelled) {
    
                        NSLog(@"Cancelled");
    
                    } else
    
                    {
                        NSLog(@"Done");
                    }
    
                    [controller dismissViewControllerAnimated:YES completion:Nil];
                };
                controller.completionHandler =myBlock;
    
                [controller setInitialText:@"Check out my Christmas Gift!"];
                [controller addImage:@"gift.jpg"];
    
                [self presentViewController:controller animated:YES completion:Nil];
    
            }
    

    You just try follow below tutorials

    1. Tutorial 1

    2.Tutorial 2