1

I am using the following code to post something on Facebook.

- (IBAction)post:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
    mySocialComposer = [[SLComposeViewController alloc]init];
    mySocialComposer = [SLComposeViewController 
    composeViewControllerForServiceType:SLServiceTypeFacebook];
    [mySocialComposer setInitialText:@"Hellooooooo World"];
    [mySocialComposer addImage:[UIImage imageNamed:@"image.jpg"]];

    [self presentViewController:mySocialComposer animated:YES completion:nil];


[mySocialComposer setCompletionHandler:^(SLComposeViewControllerResult result){

    NSString *outout = [[NSString alloc] init];

    switch (result) {
        case SLComposeViewControllerResultCancelled:
            outout = @"Post Cancled";
            break;
            case SLComposeViewControllerResultDone:
            outout = @"Post Successfull";

        default:
            break;
    }


    UIAlertView *myalertView = [[UIAlertView alloc]initWithTitle:@"FaceBook" 
    message:outout delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [myalertView show];

}];
}

}

I am using the above method to post on Facebook from iOS app.If I do post including an Image and some text it successfully post ed on Facebook. But when I try to post on Facebook without the image just text for that purpose i comment out the following line.

  [mySocialComposer addImage:[UIImage imageNamed:@"image.jpg"]];

And while doing that i got an error alert that This post cannot be sent because connection to Facebook lost. Is there any way that i can post on Facebook without image . Thanks in advance.

miken32
  • 42,008
  • 16
  • 111
  • 154
Usman
  • 61
  • 2
  • 8

2 Answers2

2

You can just set the add image to nil. You should be able to post fine.

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController* mySocialComposer = [SLComposeViewController
                            composeViewControllerForServiceType:SLServiceTypeFacebook];
        [mySocialComposer setInitialText:@"Hellooooooo World"];
        [mySocialComposer addImage:nil];

        [self presentViewController:mySocialComposer animated:YES completion:nil];


        [mySocialComposer setCompletionHandler:^(SLComposeViewControllerResult result){

            NSString *outout = [[NSString alloc] init];

            switch (result) {
                case SLComposeViewControllerResultCancelled:
                    outout = @"Post Canceled";
                    break;
                case SLComposeViewControllerResultDone:
                    outout = @"Post Successfull";

                default:
                    break;
            }
gabemorales
  • 146
  • 6
1

As per Apple's official documentation on SLComposeViewController you will see that it says

Return Value

Returns a Boolean value indicating whether the text was successfully set. Discussion

This method returns NO if text does not fit in the currently available character space or if the view controller has already been presented to the user (and therefore cannot be changed). Character limits are dependent on the target service and are documented by the service provider. For links to documentation for the supported services, see “Table 1Social Services Individual Documentation Sites” in SLRequest Class Reference.

So if this controller has already been shown it is not going to work. Try a different text and see again.

Can you remove

mySocialComposer = [[SLComposeViewController alloc]init];

You are not supposed to do that.

[mySocialComposer setInitialText:@"Hellooooooo World"];
[mySocialComposer addImage:[UIImage imageNamed:@"image.jpg"]];

[self presentViewController:mySocialComposer animated:YES completion:nil];

Put these lines of code after you set your completion handler.

Also remove the UIAlertView from there and put it inside the completion handler for error case.

S.P.
  • 3,054
  • 1
  • 19
  • 17
  • 1
    Yes i have got 1 in return value but when i comment out the add image line and run the app and press the post button in addition to the error alert i also got an error message in console ": CGImageCreate: invalid image size: 0 x 0." – Usman Oct 05 '12 at 06:37
  • Found it remove these lines from your code "mySocialComposer = [[SLComposeViewController alloc]init];" – S.P. Oct 05 '12 at 06:40
  • 1
    Still got the same error ": CGImageCreate: invalid image size: 0 x 0." – Usman Oct 05 '12 at 06:53
  • did you find anything else regarding my issue? – Usman Oct 05 '12 at 07:44
  • See if this works for you and then comment out. I can't find any reason for this not to work in your code. http://stackoverflow.com/questions/12720087/ios6-facebook-integration-image-post – S.P. Oct 05 '12 at 07:55
  • Also did you try my other suggestion where I asked you to put the text, images and presentviewcontrollers below the completion handler? – S.P. Oct 05 '12 at 07:57
  • 1
    yes i did that too i had put all things down as per you instruction. But it did't solve my problem. Does it works on your system? – Usman Oct 05 '12 at 08:03
  • after adding the image using this addImage what thumbnail would you see on the posting window? I see a safari-like icon. I suppose I should see a miniature of the image I am sending with a clips or something... but it is not showing like that. – Duck Oct 14 '12 at 03:53