ViewController presentViewController crashes on iOS 8.3 (12F70)

416 Views Asked by At

I have been struggling with this issue for three days: The sample code which I got from SO works on ios7.11 but crashes with strange Crash log:

The Code is:

UIImage *image=[UIImage imageNamed:@"wishlist_active.png"];
NSString *str=@"Image form My app";
NSArray *postItems=@[str,image];

UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil];

//if iPhone
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    [self presentViewController:controller animated:YES completion:nil];
}
//if iPad
else {
    // Change Rect to position Popover
    UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
    [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

The crash log on the iOS 8.3 is :

2015-06-22 18:09:22.706 MyApp[827:61605] *** Terminating app due to uncaught exception 'NSRangeException, reason: *** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array

*** First throw call stack:

(0x2341afef 0x31968c8b 0x2332d821 0x27120261 0x271202b7 0x26d0aab1 0x26e03dcd 0x26e037a7 0x26a6fb8f 0x26a6f8fd 0x26c3fb0b 0x26a6fb8f 0x26a6f8fd 0x26d61677 0x26d5c949 0x26d5da83 0x26d5f5e7 0x26b4ff5f 0x143c15 0x26aa0e2b 0x26aa0dd1 0x26a8b9c3 0x26aa083d 0x26aa0517 0x26a99df1 0x26a6ffe5 0x26ce68fb 0x26a6e9f9 0x233e0faf 0x233e03bf 0x233dea25 0x2332b201 0x2332b013 0x2ac0a201 0x26acfa59 0x144c11 0x31ef4aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
1

There are 1 best solutions below

2
On

I've checked your code on 8.3 and it works without any crash. So, i recommend you to put NSParametrAssert(); for items on array to check, if everything is ok. Even thought problem is caused by another part of code, it's good practise to check items throw NSParametrAssert before put it into array with literal initialisation.

If you use navigation controller try to present actiity VC from navigationController like so:

 UIImage *image=[UIImage imageNamed:@"images.png"];
    NSString *str=@"Image form My app";
    NSParameterAssert(image && str);
    NSArray *postItems=@[str,image];

    UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil];

    NSParameterAssert(controller);
    //if iPhone
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [self.navigationController presentViewController:controller animated:YES completion:nil];
    }
    //if iPad
    else {
        // Change Rect to position Popover
        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
        [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }

Hope this helps.