0

I have a button that shares twitter message. The problem is social network does not work on iOS 5.1 so my question is how do I send an error message if the user is using iOS 5.1?

-(IBAction)Twitter:(id)sender{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {

    SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

    SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultCancelled) {

            NSLog(@"Cancelled");

        } else

        {
            NSLog(@"Done");
        }

        [controller dismissViewControllerAnimated:YES completion:Nil];
    };
    controller.completionHandler =myBlock;

    [controller setInitialText:@"#VOX"];
    [controller addURL:[NSURL URLWithString:@""]];
    [controller addImage:[UIImage imageNamed:@""]];

    [self presentViewController:controller animated:YES completion:Nil];

}
else{
    alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please check your Twitter settings." delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil ,nil];

    [alert show];


}

}

This is my code.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
moo
  • 45
  • 5
  • If you DO want to use Twitter in 5.1, you could use the Twitter.framework – esreli Jan 08 '13 at 19:12
  • you would still have to check for 4.0 or less, however – esreli Jan 08 '13 at 19:12
  • No,I rather user social network to be honest. Just need to know how to check is user has ios6 or higher and if not an error will show up. – moo Jan 08 '13 at 19:15
  • The title of this question should be: "I want this button to show an error message if the Social framework isn't available". – rmaddy Jan 08 '13 at 20:10

2 Answers2

4

If you are supporting iOS 5.1 as your deployment target, not allowing the user to post their tweet is a terrible user experience. Instead, your method should look something like this:

- (IBAction)sendTweetTapped:(id)sender {

   if ([SLComposeViewController class]) {
      // Execute your code as you have it
   }
   else {
      // Use TWTweetComposeViewController and the Twitter framework
   }
}

You'll need to weakly link the Social framework. In doing so, if the user's iOS version doesn't support the Social framework (i.e. is less than 6.0), you're basically just sending a message to nil, which is allowed. In such a case, you'd fall back to using the Twitter framework and everyone gets to happily tweet!

** NOTE: I changed the name of your method because it's terrible and doesn't describe what-so-ever what the method is supposed to do.

jmstone617
  • 5,707
  • 2
  • 24
  • 26
-1

To solely get the system version, you can find a good answer already here: How can we programmatically detect which iOS version is device running on?

To sum it up, however, you can call:

[[[UIDevice currentDevice] systemVersion] floatValue];

Which returns the iOS version as a float value.

This, however, is a bad practice for what you need it for. It is better to check for a feature as well as checking for the current OS.To fully successfully integrate Twitter you should consider including built in Twitter functionality for iOS 5.0 as well (You will need to weakly include and #import both Twitter.framework and Social.framework):

float osv = [[[UIDevice currentDevice] systemVersion] floatValue];

if (osv >= 6.0 && [SLComposeViewController class]) { //Supports SLComposeViewController, this is preferable.

   if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
       //Success, you can tweet! (using the class SLComposeViewController)
   } else {
       if ([TWTweetComposeViewController canSendTweet]) { //Perhaps redundant, but worth a try maybe?
            //Success, you can tweet! (using the class TWTweetComposeViewController)
       } else {
            //Error Message 
       }
   } 

} else if (osv < 6.0 && osv >= 5.0 && [TWTweetComposeViewController class]) {

   if ([TWTweetComposeViewController canSendTweet]) {
        //Success, you can tweet! (using the class TWTweetComposeViewController)
   } else {
        //Error Message 
   }

} else {
       //No internal solution exists. You will have to go with 3rd party or write your own.
}
Community
  • 1
  • 1
esreli
  • 4,993
  • 2
  • 26
  • 40
  • osv would evaluate to true for 6.0, which allows for using the Social framework. 5.1 cannot use the Social framework, however, and will crash. – jmstone617 Jan 08 '13 at 19:22
  • This is the WORST possible way to solve this problem. DO NOT do this. Please use the solution given by jmstone. It is ALWAYS better to check for a specific feature than to look at the absolute iOS version. Please read the "SDK Compatibility Guide" for iOS. This talks about how to properly check for features. – rmaddy Jan 08 '13 at 20:08
  • I would frankly agree, @rmaddy it is better to check for features. I was simply answering the question that was asked which was to determine what is the current version of the OS. I'll update my answer. – esreli Jan 08 '13 at 22:18
  • Based on the comments, it is obvious the OP didn't really ask the right question originally and should have asked about how to check for the Social framework. It's not always best to blindly answer a question when it is easy to tell that the wrong question is being asked. Part of making an answer a good answer is to also offer the OP good advice on the best way to solve a problem. Enjoy. – rmaddy Jan 08 '13 at 23:59
  • You are right, @rmaddy I should have been more vigilant. Lighten up on the down votes though plz. I have updated the answer for the good of the question. :) – esreli Jan 09 '13 at 00:26
  • After rereading your update, I have downvoted again. The updated code is still doing the check of the OS version and in no way properly checks to see if required class or framework is available. Despite your comment (in bold), your answer still does this in the least appropriate way. This code will crash in the future if the Social framework becomes obsolete. – rmaddy Jan 09 '13 at 07:03
  • @rmaddy I disagree this time. If the social framework becomes obsolete, then the method call will return FALSE resulting in executing the code within } else { . TWTweetComposeViewController is likely to go obsolete first and even that won't cause the app the crash, with proper error handling of course. – esreli Jan 09 '13 at 07:47
  • No, if the `SLComposeViewController` doesn't exist, the code will crash, not return `nil`. This is why I stated earlier that the check should be done like the code in jmstone's answer. Check for classes and methods, not OS versions. It's the proper way to handle such things. It's how Apple documents it. – rmaddy Jan 09 '13 at 07:50