UIActivity custom activityViewController crash on iPad

555 views Asked by At

I have a custom UIActivity that I use in order to create a Contact to the device's AddressBook. In this UIActivity, I create an ABNewPersonViewController, put it inside a UINavigationController and return it in UIActivity's

- (UIViewController *)activityViewController

The problem is that on the iPad I get a crash due to a reference to a released UINavigationController. Messages are of type:

*** -[UINavigationController _viewControllerForSupportedInterfaceOrientations]: message sent to deallocated instance 0xa6f1660

Also after my controller is dismissed, the iPad does not autorotate the view when the interface changes orientation.

The code I use is the following:

In UIActivity

- (void)prepareWithActivityItems:(NSArray *)activityItems
{    
    // Prepare the AB View Controller
    ABRecordRef aContact = ABPersonCreate();
    CFErrorRef error = NULL;

    ABRecordSetValue(aContact, kABPersonKindProperty, kABPersonKindOrganization, &error);
    ABRecordSetValue(aContact, kABPersonOrganizationProperty, @"Apple Inc.", &error);

    ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(phone, @"+1 2345 784513", kABWorkLabel, NULL);
    ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &error);
    CFRelease(phone);

    self.newContactVC.title = @"New company";
    self.newContactVC.displayedPerson = aContact;
    [self.navigation setViewControllers:[NSArray arrayWithObject:self.newContactVC]];

    CFRelease(aContact);
}

- (UIViewController *)activityViewController
{   
    return self.navigation;
}

// Dismisses the new-person view controller.
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
    [self activityDidFinish:YES];
}

In the ViewController that calls this UIActivity

- (IBAction)showActionsSheet:(id)sender {        
    AddToAddressBookActivity *abActivity = [[AddToAddressBookActivity alloc] init];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:nil
                                                                             applicationActivities:[NSArray arrayWithObject:abActivity]];

    activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];

    if (!self.popover || ![self.popover isPopoverVisible]) {
        self.popover = [[UIPopoverController alloc] initWithContentViewController:activityVC];
        [self.popover setDelegate:self];
        self.popover.passthroughViews = nil;
        [self.popover presentPopoverFromRect:self.showASBtn.frame
                                      inView:self.view
                    permittedArrowDirections:UIPopoverArrowDirectionAny
                                    animated:YES];
    }
}

Any help would be greatly appreciated.

Link to a demo project: http://ge.tt/23MeOYq/v/0?c

1

There are 1 answers

0
archsten On

I had a similar problem, except the crash occurred on rotation after the activity view controller was dismissed. In my case, I had the custom view controller in a storyboard setup to present as "fullscreen", and on changing it to display as "form sheet" the crash went away. I don't know why that made a difference though.

Regarding your autorotation not working after the custom view controller dismisses, try adding a completion handler to your activity view controller to nil your popover view controller ivar. I noticed that if I didn't do this the custom activity and the custom view controller don't get dealloc'd, and autorotation doesn't happen. e.g.:

avc.completionHandler = ^(NSString* activityType, BOOL completed){
    if ( UIUserInterfaceIdiomPad == [UIDevice currentDevice].userInterfaceIdiom )
        self.popover = nil;
};

Also, check out the post at: "activityviewcontroller not dismissing", which I found helpful.