29

[Appears to be fixed in iOS 10!] So what follows applies to iOS 9 only...


I have been experimenting with Apple's new Contacts framework, and I've found a huge bug in one of the three forms of CNContactViewController. It destroys the surrounding interface so that your app becomes useless; the user is stuck.

To make this bug easy to see, I've posted an example project at https://github.com/mattneub/CNContactViewControllerBug.

To experiment, run the project and do the following steps:

  1. Tap the button (Unknown Person).

  2. Grant access if requested.

  3. You are shown the partial contact, in our navigation interface (note the Back button at the top).

  4. Tap Add to Existing Contact. The contact picker appears.

  5. Tap Cancel. It doesn't actually matter what you do from here, but tapping Cancel is simplest and is the fastest way to reach the bug.

  6. We are now back at the partial contact, but the navigation interface is gone. The user has no way to escape from this interface. The app is hosed.

Just to clarify, here are screenshots of the steps you need to take:

enter image description here

Tap Add to Existing Contact to see this:

enter image description here

Tap Cancel to see this; observe that it is the same as the first screen shot, but the navigation bar is gone:

enter image description here

I've tried many ways to work around this bug, but there seems to be no way. As far as I can tell, this window is being presented by the framework "out-of-process" and is not part of your app. You can't get rid of it.

So what's the question? I guess it's this: can anyone show me a way to make this view controller (in this form) usable? Is there a workaround I haven't found?

EDIT This bug appeared in iOS 9.0 and is still present in iOS 9.1. In a comment, @SergeySkopus reports that switching to the deprecated Address Book framework doesn't help; the bug is in the underlying structure somewhere.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • These tests were performed using iOS 9 and (on the device) iOS 9.0.2. Reported to Apple as radar 22984883. – matt Oct 06 '15 at 15:01
  • 1
    If you find a work-around, please do update this post. Have similar issues. It's even worse on iPad, where if you use [CNContactViewController viewControllerForContact:] and then try to edit a birthday, the same thing happens ... the navigation bar disappears completely, leaving the user stuck on this screen. It doesn't look like the issue was fixed in iOS9.1 (beta3) either. – Z S Oct 07 '15 at 06:23
  • @ZS I presume you've filed bugs? – matt Oct 07 '15 at 06:25
  • I did. Don't know when the Contacts team will get around to fix them though. Haven't seen it fixed in 9.1 betas. – Z S Oct 07 '15 at 16:51
  • @ZS Excellent, thanks. I think the problem here is that the view controller that pop up in the middle of editing, such as All Contacts list in my screen shot above, are now injected "out of process"; they don't even appear when you do View Debugging. – matt Oct 07 '15 at 17:12
  • 1
    @matt: Navigation should return if you `Create New Contact` > `Cancel` – l'L'l Oct 08 '15 at 09:07
  • @l'L'l The problem is that the navigation is gone. Even in code I can't pop back to my own interface. The app is hosed. – matt Oct 08 '15 at 14:43
  • 1
    @matt: Yes indeed — it's quite mucked, as the navigation flat out leaves the building with elvis. It's a seriously bad bug unfortunately with no solution that I can see beyond Apple actually fixing it. One thing that *might* be worth trying is to setup another UINavigationBar above the disappearing one and not allow the view to use the entire screen, although it will likely vanish as well perhaps. – l'L'l Oct 08 '15 at 14:50
  • @l'L'l I'm glad not I'm not the only one. :) – matt Oct 08 '15 at 15:11
  • @matt: I tried adding another UINavigationBar, and adjusting the view to no avail; the "Add to Existing Contact" view just shoves the navigation right off the screen — and there seems to be no way to control any aspect of it. I'll file a bug report also... – l'L'l Oct 08 '15 at 15:24
  • @matt I'm experiencing the same problem. Only the "Create New Contact" seems to retain the navigation bar and the back button. – The Dude Oct 09 '15 at 14:59
  • Another annoying thing is that I see an empty white page while showing the `CNContactViewController` in the Xcode simulator (Xcode Version 7.0.1 (7A1001)). Does somebody see this problem as well. What can be the possible reason and fix for this problem? – The Dude Oct 09 '15 at 15:07
  • @TheDude Might be good to ask that as a separate question. – matt Oct 09 '15 at 16:13
  • The same problem is experienced with deprecated AB API. The QuickContacts example has the same bug – Sergey Skopus Oct 21 '15 at 12:09
  • @SergeySkopus Is that the case if you run the QuickContacts example on iOS 8 too? In other words, was this always true - if so, I'm surprised I never noticed it - or is this a new bug in iOS 9? – matt Oct 21 '15 at 13:43
  • it is a new bug in ios 9. ios 8 works fine – Sergey Skopus Oct 21 '15 at 15:11
  • Thanks @SergeySkopus. I feel better knowing this - otherwise, I'd be worried that I never noticed the bug in iOS 8 and before using the Address Book framework. :) So they've changed something about the underlying mechanism, and reverting to the Address Book framework won't work around it, because it's the same underlying mechanism either way. That is very good info. Thanks! – matt Oct 21 '15 at 15:14
  • I've submitted this bug to Apple under Radar 28099522. – Andrew Aug 31 '16 at 18:33
  • 1
    Thanks @Andrew - you might want to add a note calling attention to my existing bug Radar 22984883. – matt Aug 31 '16 at 19:01
  • I heard from Apple Developer Technical Support. I used one of my two TSI's for the year to ask them to address this. They say they could find no known workaround and that the engineering team is aware of the problem and are working to fix it in a "future update." I guess I have to build a contact view from scratch if I want to update my app any time soon. – Andrew Sep 10 '16 at 05:10
  • @Andrew thanks for reporting back. This is outrageous. – matt Sep 10 '16 at 12:07
  • I am facing this similar issue in iOS 12 and 12.0.1. The workaround(not exactly a workaround) that work for me, is to kill the app and do the step again and it magically appears. – hirenhcm Oct 11 '18 at 07:21

7 Answers7

8

I've hidden the UINavigationController method for show or hide the navigation bar by using categories:

@interface UINavigationController (contacts)
@end

@implementation UINavigationController (contacts)

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated {
    NSLog(@"Hide: %d", hidden);
}
@end

This way the CNContactViewController cannot make the navigation bar to disappear. Setting a breakpoint on NSLog I discovered that this method is called by the private [CNContactViewController isPresentingFullscreen:].

By checking if the self.topViewController of the navigation controller is kind of class CNContactViewController you could decide if hiding or not the navigation bar.

Giammy
  • 81
  • 1
  • 3
  • 1
    Extremely clever! The problem is that now the navigation bar doesn't vanish when you summon the list of contacts (e.g. you tap Add To Existing Contacts). That's wrong. But you've certainly shown where the issue is: the list of contacts should appear in front of _everything_, but instead it appears only in front of the navigation controller's child view. That must be why they are hiding the navigation bar instead. – matt Dec 15 '15 at 17:13
  • Unfortunately the solution as posted here has side-effects, such as keeping the send button disabled in `MFMailComposeViewController`. I suppose method-swizzling could be used to be able to call the original `setNavigationBarHidden:animated:` method. – newenglander Jun 14 '16 at 10:41
7

Evidently this is a bug, since Apple has finally responded to my bug report by declaring it a duplicate.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 2
    @mattt can you please share link for that reported bug – Jaydeep Patel Jan 12 '16 at 06:13
  • Here is one I found on [Open Radar](https://openradar.appspot.com/23971668). I have same issue using UIPopoverPresentationController on iPhone (on iPad it's perfect)... – Shebuka May 23 '16 at 14:00
5

The only way I found to make "CNContactViewController forUnknownContact" usable is to abandon the navigationbar and use a toolbar to exit modal view like this (in Objective C):

CNContactViewController *picker = [CNContactViewController viewControllerForUnknownContact: newContact];
picker.delegate = self;

UINavigationController *newNavigationController = [[UINavigationController alloc] initWithRootViewController:picker];

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleDone target:self action:@selector(YourDismissFunction)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[picker setToolbarItems:[[NSArray alloc] initWithObjects:flexibleSpace, doneButton, flexibleSpace, nil] animated:NO];

newNavigationController.toolbarHidden = NO;
picker.edgesForExtendedLayout = UIRectEdgeNone;

[self presentViewController:newNavigationController animated:YES completion:nil];

hoping that it could help

Roudger
  • 61
  • 1
  • 3
  • Very clever, and it does give the user a way out of the interface — the navigation bar is destroyed, but the toolbar is not, so the Done button remains present and operative. Unfortunately this also gives us an empty navigation bar. Even if you hide the navigation bar to start with, it reappears if you do Create New Contact followed by Cancel. I think I like the other workaround better, because of this. – matt Jan 18 '16 at 17:06
  • Very helpful, I just used `initWithBarButtonSystemItem:target:action:`instead of `initWithTitle:style:target:action:` to avoid having to translate another word. – newenglander Apr 29 '16 at 11:18
  • This is really the only thing that helped me to make it work in 03.2020. Stupid. – Bernhard Krenz Mar 10 '20 at 09:51
3

Are you interested in a very private API fix?

@implementation CNContactViewController (Debug)

+ (void)load
{
    Method m1 = class_getInstanceMethod([CNContactViewController class], NSSelectorFromString(@"".underscore.s.h.o.u.l.d.B.e.O.u.t.O.f.P.r.o.c.e.s.s));
    Method m2 = class_getInstanceMethod([CNContactViewController class], @selector(checkStatus));

    method_exchangeImplementations(m1, m2);
}

- (BOOL)checkStatus
{
    //Leo: Fix bug where in-process contact view controller crashes if there is no access to local contacts.
    BOOL result;
    if([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized)
    {
        result = NO;
    }
    else {
        result = YES;
    }

    return result;
}

@end

This is a "magic" solution that reverts Apple's use of the buggy XPC controllers. Solves so many issues in both the modern CN controllers, as well as legacy AB controllers, which use the CN ones internally.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
0

This is one of those issues I was glad to see I was not alone.

I have the same issue when displaying a contact using CNContactViewController(contact:).

When the image or 'share contact' was tapped the navigation bar in the root CNContactViewController would disappear making the user stuck. This has not been fixed as of iOS 9.3.3.

The solution for me at this point in time is to use the uitoolbar. The problem is that this appears at the bottom all the time, even with the image data for the contact in full screen.

// initialise new contact view controller to display with contact
                let contactVC = CNContactViewController(forContact: contact!)

                // set view controller delegate
                contactVC.delegate = self

                // set view controller contact store
                contactVC.contactStore = self.store

                // enable actions
                contactVC.allowsActions = true

                // disable editing
                contactVC.allowsEditing = false

                // add cancel button
                let cancelButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: #selector(dismissContactVC(_:)))

                // add flexible space
                let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)

                // add to toolbar
                contactVC.setToolbarItems([flexibleSpace, cancelButton, flexibleSpace], animated: false)

                // contact view controller must be embedded in navigation controller
                // initialise navigation controller with contact view controller as root
                let navigationVC = SubClassNavigationController(rootViewController: contactVC)

                // show toolbar
                navigationVC.setToolbarHidden(false, animated: false)

                // set navigation presentation style
                navigationVC.modalPresentationStyle = UIModalPresentationStyle.CurrentContext

                // present view controller
                self.presentViewController(navigationVC, animated: true, completion: nil)

After this a blank navigation bar appears when you first present the cncontactviewcontroller so to remove this I subclassed uinavigationcontroller, and in viewWillAppear(animated:) I call the function setnavigationbar(hidden: animated:) to hide the navigation bar.

I hope Apple fixes this soon as this is a less than ideal solution.

Itergator
  • 299
  • 3
  • 16
-1

This problem can easily solved. Subclass CNContactViewController and in the viewDidAppear method first call the super class and then immediately following set the leftBarButtonItem with an action method that calls dismissViewController. Also make sure that you embed that viewController into a navigation controller.

mcoding
  • 9
  • 2
-1

Well, I found three ways to solve the problem TEMPORARILY.

Swift 2.2 Version:


Option 1: Shake device to show navigation bar or dismiss directly

class CustomContactViewController: CNContactViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        UIApplication.sharedApplication().applicationSupportsShakeToEdit = true
    }

    override func canBecomeFirstResponder() -> Bool {
        return true
    }        

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)            
        becomeFirstResponder()
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)            
        resignFirstResponder()
        UIApplication.sharedApplication().applicationSupportsShakeToEdit = false
    }

    override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
        navigationController?.setNavigationBarHidden(false, animated: true)

        // or just dismiss
        // dismissViewControllerAnimated(true, completion: nil)

        // or pop
        // navigationController?.popViewControllerAnimated(true)

    }
}


Option 2: Set a timer to force the navigation bar to show. But... it also creates a new problem, you cannot edit or share the contact avatar.

class CustomContactViewController: CNContactViewController {

    var timer: NSTimer?

    override func viewDidLoad() {
        super.viewDidLoad()
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(showNavigationBar), userInfo: nil, repeats: true)
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        timer?.fire()
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        timer?.invalidate()
    }

    @objc private func showNavigationBar() {
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
}


Option 3: Create a dismiss button on the top most view.

class CustomContactViewController: CNContactViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configureDismissButton()
    }

    private func configureDismissButton() {

        guard let topView = UIApplication.topMostViewController?.view else { return }

        let button = UIButton()
        button.setImage(UIImage(named: "close"), forState: .Normal)
        button.addTarget(self, action: #selector(dismissViewController), forControlEvents: .TouchUpInside)
        topView.addSubview(button)

        // just use SnapKit to set AutoLayout
        button.snp_makeConstraints { (make) in
            make.width.height.equalTo(36)
            make.bottom.equalTo(8)
            make.left.equalTo(-8)
        }
    }

    @objc private func dismissViewController() {
        dismissViewControllerAnimated(true, completion: nil)
    }

    var topMostViewController: UIViewController? {
        var topController = UIApplication.sharedApplication().keyWindow?.rootViewController
        while topController?.presentedViewController != nil {
            topController = topController?.presentedViewController
        }
        return topController
    }
}

enter image description here

iAugus
  • 69
  • 1
  • 6