EXC_BAD_ACCESS代码= 2地址= 0x0

时间:2014-05-05 17:34:31

标签: ios exc-bad-access nszombieenabled

我在我的应用程序中使用UIActivityViewController,我在iOS 6上遇到EXC_BAD_ACCESS代码= 2崩溃,但iOS 7没有。这是代码:

NSArray *activityItems;

NSString *shareText = [NSString stringWithFormat:NSLocalizedString(@"Listen to", nil), self.currentChannel.title, self.currentChannel.itunesUrl];

if (self.currentChannel.mediumThumbnailImage)
{
    activityItems = @[shareText, self.currentChannel.mediumThumbnailImage];
}
else
{
    activityItems = @[shareText];

}

UIActivityViewController *activityController = [[UIActivityViewController alloc]
                                                initWithActivityItems:activityItems
                                                applicationActivities:nil];

[activityController setCompletionHandler:^(NSString *activityType, BOOL completed) {
    // once they have shared, check where they shared the content for analytics
    if (completed)
    {
        NSString *actionName = nil;
        NSString *socialName = nil;
        if ([activityType isEqualToString:kMailActivity]) {
            actionName = kSocialEmail;
            socialName = kMail;
        } else if ([activityType isEqualToString:kMessageActivity]) {
            actionName = kSocialChat;
            socialName = kMessage;
        } else if ([activityType isEqualToString:kFacebookActivity] || [activityType isEqualToString:kTwitterActivity]) {
            actionName = kSocialShare;
            socialName = kFacebook;
        }

        if (actionName && socialName)
        {
            NSDictionary *data = @{kSocialName: socialName, kSocialContent: shareText};
            if (data)
            {
                [ADBMobile trackAction:actionName data:data];
            }
        }
    }
}];

if (activityController)
{
    [activityController setExcludedActivityTypes:
     @[UIActivityTypeAssignToContact,
       UIActivityTypePrint,
       UIActivityTypePostToWeibo,
       UIActivityTypeSaveToCameraRoll,
       UIActivityTypeAirDrop]];

    [self presentViewController:activityController
                       animated:YES completion:nil];
}

我已经使用NSZombies来缩小发生崩溃的位置,当我在iOS 6中调用setExcludedActivityTypes:时就会发生这种情况。我知道这个错误意味着一个对象被过度释放,我正在触摸内存那不属于我。我不明白为什么这次崩溃只发生在iOS 6中。有没有人看到可能导致这种情况的事情?

1 个答案:

答案 0 :(得分:1)

UIActivityTypeAirDrop仅适用于iOS 7,而不适用于iOS 6。

您可以检查常量的可​​用性,如下所示:

if(&UIActivityTypeAirDrop) {
    // UIActivityTypeAirDrop is available
} else {
    // Its not available. Don't use it.
}

(我正在制作社区维基,因为我刚刚复制了上面用户Larme的评论。)