简体   繁体   中英

UIActivityViewController crashes when cancelling on iPhone iOS8

I am showing activityViewController using this line of code from my ViewController

[MyClass showShareMenuWithString:@"test string" sender:self];

This is a declaration of this method inside MyClass

+ (void)showShareMenuWithString:(NSString*)text sender:(id)sender{
    NSArray* array = [NSArray arrayWithObject:[NSString stringWithFormat:@"%@",text]];
    UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:array applicationActivities:nil];

    [sender presentViewController:activityView animated:YES completion:nil];

}

After this, activityView appears on the screen with an ability to select some actions like sending SMS or Email and others. After choosing one of this actions, SMS of Mail controller is presenting on the screen with the prefilled data. But after cancelling this controllers, my app crashes with the EXC_BAD_ACCESS error. It happens randomly, and there's a little chance that this controllers would disappear without crash. Application is iPhone only and array is not a nil.

The app didn't crash on iOS7. It was tested on iPhones with the different iOS 7/8 . Has somebody the same problems? Is there any find out ?

Thanks to @Y.Bonafons

I didn't set my delegate to nil in the previous controller after pop'ing, that's why it was trying to call it's methods.

Enabling Zombie Objects helps me a lot.

Another problem can can be related with a UIActivityViewController crash could be an unexpectedly found nil while unwrapping an Optional value.

In the closure that is assigned to act as the completion handler to be executed when the Action extension returns control to the application, be sure to check for the optional before to use it.

For example, use optional binding to find out whether the optional returnedItems contains a value (naïf code):

activityViewController.completionWithItemsHandler =
            { (activityType, completed, returnedItems, error) in

    if let a = returnedItems {
                    ...
                } else {
                    return
                }
...

This is just a way to try, but surely you can write something better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM