2

Apparently, my use of Twitter oAuth (token request) doesn't work in iOS 5... how can I keep this code for anything below iOS 5 and use the new Twitter Framework for iOS 5+?

Is it possible to detect iOS versions?

Thanks!

Adam Storr
  • 1,438
  • 2
  • 21
  • 46
  • The only time you should be looking at the iOS Version is for tracking customer usage. Note that this is also considered a personally identifying statistic and shouldn't be examined without a fair request to "Get users' permission to collect data" and from that point you may only send it to yourself for analytics purposes not to a 3rd party. As mentioned in the answer below, query functionality not the OS Version. – Dru Freeman Apr 04 '12 at 14:36

4 Answers4

6

You (almost) never want to query iOS (or even framework) versions. That (usually) means you're solving the wrong problem.

In this case, you really want to know "can I use Twitter.framework?"

Thanks to the magic of weak linking, you can try something like:

if ([TWTweetComposeViewController canSendTweet]) {
    // Do something
}
else {
    // Use your original code
}

You can also check for lower level framework components, e.g.:

if ([TWRequest class]) {
    // Do something
}
else {
    // Use your original code
}

(Obviously you will need to link against Twitter.framework and include the requisite headers.)

Conrad Shultz
  • 8,748
  • 2
  • 31
  • 33
1
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {
   //tweet
}
jacekmigacz
  • 789
  • 12
  • 22
  • Note that this is unsatisfactory for a general comparison due to string truncation during float conversion. And in general you shouldn't ever use systemVersion for much of anything anyhow. – Conrad Shultz Apr 04 '12 at 08:31
  • According IEEE 754-1985 standard, positive integral values represented by floating point number less then 7 are guarantied to be lengthened. To prove it, print out a value of FLT_DIG from float.h. – jacekmigacz Apr 04 '12 at 08:40
  • I think we're talking different issues: NSString *v1 = @"5.1"; NSString *v2 = @"5.1.1"; NSLog(@"%@ is greater than %@: %d", v2, v1, ([v2 floatValue] > [v1 floatValue])); 2012-04-04 01:41:44.795 VersionTest[3624:f803] 5.1.1 is greater than 5.1: 0 – Conrad Shultz Apr 04 '12 at 08:42
  • (Sorry, comments are quite whitespace unfriendly.) – Conrad Shultz Apr 04 '12 at 08:43
  • Oh. Sure. 5.1.1 will become 5.1. But the question is about Twitter and even truncated and rounded version number is sufficient here. – jacekmigacz Apr 04 '12 at 08:47
  • 1
    I realize that, which is what I meant by "general comparison" (in case future readers come across this and thought this would be more broadly applicable). But the bigger point (to me) is that the only reason I can ever think of to use systemVersion is to present a warning about *future* incompatibilities; current compatibility ought to be established with specific run-time checks. – Conrad Shultz Apr 04 '12 at 08:51
1

First and foremost, the other answers are correct-- you should avoid using iOS version number to check if features exist.

HOWEVER: In case you do indeed have a good reason to check iOS version, my all-time favorite answer for checking iOS version number is in this StackOverflow answer. So elegant.

Community
  • 1
  • 1
codeperson
  • 8,050
  • 5
  • 32
  • 51
0

Detect if the Twitter class is in the installed os :

if (NSClassFromString(@"TWTweetComposeViewController")) {
   //use Twitter Framework
}

Do not forget to make the Twitter Framework optional in the list of Frameworks.

Michaël
  • 6,676
  • 3
  • 36
  • 55
  • Not needed in the modern run-time; just weak link the framework. This way you can get compile-time checks for typos, etc., too. – Conrad Shultz Apr 04 '12 at 08:22