如果iOS 6 [这样做]其他iOS 5 [那样做]? - UIActivityViewController

时间:2012-12-05 20:44:12

标签: iphone objective-c ios xcode uiactivityviewcontroller

我有一个 UIActivityViewController ,可以在Facebook / Twitter / Email上分享文字和图像,但它只能在iOS 6+上运行..是否可以选择在iOS上运行的iOS版本设备并执行IF语句以避免在iOS 5中崩溃?如果是iOS6,请执行以下代码,否则执行其他操作......?

-(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];
}

3 个答案:

答案 0 :(得分: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];
}

或者更好地使用

if( NSClassFromString (@"UIActivityViewController") ) {

}

阅读this

答案 1 :(得分:4)

有几种方法可以做到这一点,但最简单的方法可能是测试这个类的存在,例如:

if ([UIActivityViewController class])
  //iOS 6+
else 
  // < iOS 6

答案 2 :(得分:0)

您应该考虑使用[[UIDevice currentDevice] systemVersion]来返回当前操作系统的字符串。您可以轻松编写宏来查看版本是否至少是指定的数字。