Question

I am using a UIActivityViewController in my app, and I am getting a EXC_BAD_ACCESS code=2 crash on iOS 6, but not iOS 7. Here is the code:

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

I have used NSZombies to narrow down where the crash is happening, and it is happening when I call setExcludedActivityTypes: in iOS 6. I know that this error means that an object has been overreleased, and I am touching memory that doesn't belong to me. What I don't understand is why this crash is only occurring in iOS 6. Does anyone see something that could be causing this?

Was it helpful?

Solution

UIActivityTypeAirDrop is only available in iOS 7 and not in iOS 6.

You can check the availability of a constant like this:

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

(I'm making this a community wiki, because I just copied the comment from user Larme above.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top