0

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?

Chandler De Angelis
  • 2,646
  • 6
  • 32
  • 45
  • 2
    `UIActivityTypeAirDrop` is available since iOS7 only. That what could be causing the crash. See there: https://developer.apple.com/library/ios/documentation/uikit/reference/UIActivity_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40011974-CH1-SW13 – Larme May 05 '14 at 17:39
  • Well, the doc says its available since iOS7. And if you don't add it in your tests, does it still crash? – Larme May 05 '14 at 17:42

1 Answers1

1

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.)

Michael
  • 6,451
  • 5
  • 31
  • 53