简体   繁体   中英

CNContactViewController can't call or send a message (show using Peek and Pop or UIViewControllerPreviewingDelegate)

I'm showing CNContactViewController in my application using Peek and Pop, it looks ok, but none of actions is working.

Here is my code. Creating the View Controller:

func previewViewController() -> CNContactViewController {
    let contactVC = CNContactViewController(for: theContact)
    contactVC.allowsEditing = false
    contactVC.allowsActions = true
    contactVC.delegate = defaultContactVCDelegate
    return contactVC
}

Implementing the UIViewControllerPreviewingDelegate :

extension MyViewController: UIViewControllerPreviewingDelegate {
    func previewingContext(_ previewingContext: UIViewControllerPreviewing,
                     viewControllerForLocation location: CGPoint) -> UIViewController? {
        let preview = previewViewController()
        let wrapper = UINavigationController(rootViewController: preview)
        return wrapper
    }

    func previewingContext(_ previewingContext: UIViewControllerPreviewing,
                     commit viewControllerToCommit: UIViewController) 
    {
        guard let wrapper = viewControllerToCommit as? UINavigationController else { return }
        guard let preview = wrapper.viewControllers.first else { return }
        navigationController?.pushViewController(preview, animated: false)
    }
}

The CNContactViewControllerDelegate :

class DefaultContactViewControllerDelegate: NSObject, CNContactViewControllerDelegate {
    func contactViewController(_ viewController: CNContactViewController,
                         shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
        return true
    }
}

I suppose if these actions are present on the screen (I mean bottom actions and top circles), they are supposed to be touchable. But when I touch them, nothing happens, as if user interaction where disabled.

Well, as it often happens, the solution came up to me before I received an answer.

The problem was that CNContactViewController was somehow broken after previewing in previewingContext(,viewControllerForLocation) method.

So I just create and show a new controller for the same CNContact instead of using viewControllerToCommit .

Here is my working codepiece:

func previewingContext(_ previewingContext: UIViewControllerPreviewing,
                 commit viewControllerToCommit: UIViewController) 
{
    let preview = previewViewController()
    navigationController?.pushViewController(preview, animated: false)
}

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