5

我有一个UIActivityViewController在 Facebook/Twitter/Email 上共享文本和图像,但它只适用于 iOS 6+ .. 是否可以选择设备上运行的 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];
}
4

3 回答 3

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") ) {

}

这个

于 2012-12-05T20:47:29.230 回答
4

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

if ([UIActivityViewController class])
  //iOS 6+
else 
  // < iOS 6
于 2012-12-05T20:48:16.020 回答
0

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

于 2012-12-05T20:48:01.360 回答