Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if iOS 6 [do this] else iOS 5 [do that]? - UIActivityViewController

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];
}
like image 245
emotality Avatar asked Dec 05 '12 20:12

emotality


2 Answers

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

like image 163
Max Avatar answered Nov 05 '22 16:11

Max


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
like image 29
wattson12 Avatar answered Nov 05 '22 16:11

wattson12