5

I've got a UIActivityViewController that shares Text and Image on Facebook/Twitter/Email, but it only works on iOS 6+.. Is it possible to pick up the iOS version running on the device and do an IF Statement to avoid crashing in iOS 5 ? If iOS6, do the following code, else do something else...?

-(void)shareMenu
{
    NSString *textToShare = @"Text that will be shared";
    UIImage *imageToShare = [UIImage imageNamed:@"share_picture.png"];
    NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare, imageToShare, nil];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
    activityVC.excludedActivityTypes = [[NSArray alloc] initWithObjects: UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeMessage, UIActivityTypePostToWeibo, nil];
    [self presentViewController:activityVC animated:YES completion:nil];
}
John Topley
  • 113,588
  • 46
  • 195
  • 237
emotality
  • 12,795
  • 4
  • 39
  • 60
  • In general (not this specific case) I find it's usually best to use `respondsToSelector` to determine which version of a iOS class I'm dealing with, based on what methods it supports. Among other things this helps to document the specific dependency. – Hot Licks Aug 24 '13 at 13:57

3 Answers3

25
if( [UIActivityViewController class] ) {
     NSString *textToShare = @"Text that will be shared";
    UIImage *imageToShare = [UIImage imageNamed:@"share_picture.png"];
    NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare, imageToShare, nil];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
    activityVC.excludedActivityTypes = [[NSArray alloc] initWithObjects: UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeMessage, UIActivityTypePostToWeibo, nil];
    [self presentViewController:activityVC animated:YES completion:nil];
}

Or better use

if( NSClassFromString (@"UIActivityViewController") ) {

}

Read this

Max
  • 16,679
  • 4
  • 44
  • 57
  • 2
    This is a better solution than testing to iOS version numbers explicitly. – vcsjones Dec 05 '12 at 20:48
  • This works 100% thank you! But how do I hide the **UIBarButtonItem** in my **NavigationBar** when it is not running on iOS6? In iOS5 it is clickable but no function, so I want to hide it please? Thanks for your answer! – emotality Dec 05 '12 at 21:02
4

There are a few ways to do this, but the simplest is probably to test for the existence of this class, e.g.

if ([UIActivityViewController class])
  //iOS 6+
else 
  // < iOS 6
wattson12
  • 11,176
  • 2
  • 32
  • 34
0

You should consider using [[UIDevice currentDevice] systemVersion] that will return a string for the current operating system. You can easily write a macro to see if version is at least a specified number.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • 4
    This is not a good idea. It's much better to check for the existence of the desired class. It's much cleaner and direct than relying on version numbers. – rmaddy Dec 05 '12 at 21:01
  • There's an advantage to testing for the system version: It documents very clearly when you can actually remove the test again (i.e. when dropping iOS 5 support). – nschum Oct 23 '14 at 07:52