3

I have implemented Facebook sharing in my app (iOS6) and the code is as follows.

//Completion Handler

SLComposeViewControllerCompletionHandler __block completionHandler = ^(SLComposeViewControllerResult result) {
    UIAlertView *alert = nil;
    switch(result) {
        case SLComposeViewControllerResultCancelled: {
            alert = [UIAlertView alloc]initWithTitle:@"Cancelled" message:@"Your message wasn't shared" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        break;
        case SLComposeViewControllerResultDone: {
            alert = [UIAlertView alloc]initWithTitle:@"Posted" message:@"Your message was posted successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        break;
    }
}

// Posting to Facebook

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
    SLComposeViewController *fbVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    fbVC.completionHandler = completionHandler;
    [self presentViewController:fbVC animated:YES completion:nil];
}

I am testing the following situations:

  1. Internet available and user entered text and pressed post
  2. Internet available and user entered text and pressed cancel
  3. Internet not available and user entered text and pressed post.

First two works as they should. In the third situation, as expected, I get alert

"Cannot Post to Facebook" - The post cannot be sent because connection to Facebook failed.

But after I press either Try Again or Cancel button in the alert view that was presented to me, I get "Posted" alert (the completion handler type SLComposeViewControllerResultDone gets executed).

How to prevent this?

thandasoru
  • 1,558
  • 2
  • 15
  • 41

2 Answers2

1

EDIT: Well, it was simple to fix the third situation. I added the reachability class provided by Apple (available for download here.) Only code that was required is as follows:

#import "Reachability.h"

- (BOOL)internetConnected {
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable || reachability.connectionRequired); //required for iOS 7 and above
}

... 
...

case SLComposeViewControllerResultDone: {
    if(self.internetConnected) {
        alert = [UIAlertView alloc]initWithTitle:@"Posted" message:@"Your message was posted successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    } else {
        alert = [UIAlertView alloc]initWithTitle:@"Failed" message:@"Your message was not posted, no internet was available" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    }
break;
thandasoru
  • 1,558
  • 2
  • 15
  • 41
  • hi @thandasoru this is not good or favourable as even the net connection is available still it gives this typeof error if user post same content again with in some time (not know how much). How to handle that scenario ?? – The iOSDev Dec 19 '14 at 09:55
  • In my app, I use it to upload an image. So in my code, I check if the image was already uploaded, and if it is, I disable share button. If you require to share some text, you could store the text in UserDefaults and check if user tries to upload same text again. Then you can show "The text was already shared" alert. – thandasoru Dec 20 '14 at 04:08
1

Please check If you are adding URl to It(SLComposeViewController), it must be http://www.stackoverflow.com formatted otherwise it will keep showing "Cannot Post to Facebook" - The post cannot be sent because connection to Facebook failed.

HTH

Abhishek
  • 267
  • 2
  • 11