34

I try to present a CNContactPickerViewController inside a SwiftUI application using the UIViewControllerRepresentable protocol. As I already read, there seems to be a known issue for this not working, but I got it working quite ok using the workaround described here.

However, whenever the CNContactPickerViewController gets presented or dismissed resp., I get the following error in my output log:

[PPT] Error creating the CFMessagePort needed to communicate with PPT.

I tried to find explanations on this, but there seems to be no answer anywhere on the internet. Does someone know where this error comes from and what PPT is? Could this error have something to do with the CNContactPickerViewController not working properly with SwiftUI?

I noticed the error for the first time in the iOS 14 beta together with the Xcode 12 beta, and it is still present in iOS 14.2 with Xcode 12.2. I don't know if the error appears on iOS 13 as well.
I already issued a feedback report about this.

vollkorntomate
  • 638
  • 1
  • 8
  • 17
  • Addition: the error also occurs when presenting an `ABPeoplePickerNavigationController` as `UIViewControllerRepresentable`, which is deprecated but still works in iOS 14.2. However, it seems as if this error has no impact in properly presenting the view controller. – vollkorntomate Nov 14 '20 at 14:09
  • 18
    I have this issue as well when opening MFMailComposeViewController – C. Skjerdal Dec 06 '20 at 17:30
  • 1
    I want to be in good company too. The same error is shown when sticker pack is opened. – Vladimir Vodolazkiy Dec 23 '20 at 09:20
  • This is a non-answer, so I made it a comment. It seems like there are several ways to make this error appear, in more than one framework. My environment is Swift (not Swift UI) and the error seems to be cosmetic. Without more detail on what the error means, I don't know if we can isolate the problem, in so far as we can't find a workaround that prevents it from being displayed. – benc Jan 13 '22 at 03:16
  • This was printed in console, but the mail was successfully sent :) – stackich Jun 26 '22 at 17:08

1 Answers1

4

I wrote a workaround using a hosting UINavigationController and here is my code:

import SwiftUI
import ContactsUI

struct ContactPickerView: UIViewControllerRepresentable {
    
    @Environment(\.presentationMode) var presentationMode
    
    func makeUIViewController(context: Context) -> UINavigationController {
        let navController = UINavigationController()
        let controller = CNContactPickerViewController()
        controller.delegate = context.coordinator
        navController.present(controller, animated: false, completion: nil)
        return navController
    }
    
    func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
        print("Updating the contacts controller!")
    }
    
    // MARK: ViewController Representable delegate methods
    func makeCoordinator() -> ContactsCoordinator {
        return ContactsCoordinator(self)
    }
    
    class ContactsCoordinator : NSObject, UINavigationControllerDelegate, CNContactPickerDelegate {
        let parent: ContactPickerView
        public init(_ parent: ContactPickerView) {
            self.parent = parent
        }
        
        func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
            print("Contact picked cancelled!")
            parent.presentationMode.wrappedValue.dismiss()
        }
        
        func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
            print("Selected a contact")
            parent.presentationMode.wrappedValue.dismiss()
        }
    }
}

And I use it like:

Button("Select a contact") {
       openSelectContact.toggle()
}
 .sheet(isPresented: $openSelectContact, onDismiss: nil) {
       ContactPickerView()
     }
cs4alhaider
  • 1,318
  • 1
  • 17
  • 26
  • 1
    Thank you for this snippet! :) However, the error keeps showing whenever the NavigationController gets presented/dismissed. Furthermore, due to it being a workaround and effectively presenting two ViewControllers after one another, it still doesn't have the nativ look and feel that one is used to from iOS. The same issue occured in the possible solution I linked in my question above. – vollkorntomate Feb 21 '21 at 17:21
  • I vote up because help me with and similar issue. I don't try this solution for fix the warning console. Here is my code and I present my scenario, maybe can help us! https://stackoverflow.com/a/66555614/7278926 – iGhost Mar 09 '21 at 22:25