首页 > 解决方案 > List of Contacts from Contacts App in SwiftUI

问题描述

Hi I am trying to build an interface that lists all the contacts just like the Contacts and Phone app with the same UI. What I have tried so far is below. Basically I tried to implement CNContactPickerViewController from ContactsUI using the UIViewControllerRepresentable. However what I am getting is a blank white page.

struct ContactsViewController: UIViewControllerRepresentable {

    func makeUIViewController(context: UIViewControllerRepresentableContext<ContactsViewController>) -> CNContactPickerViewController {
        let controller = CNContactPickerViewController()
        controller.delegate = context.coordinator
        controller.displayedPropertyKeys = [CNContactGivenNameKey]
        return controller
    }

    func updateUIViewController(_ uiViewController: CNContactPickerViewController, context: UIViewControllerRepresentableContext<ContactsViewController>) {
        print("updating")
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }

    class Coordinator: NSObject, CNContactPickerDelegate {
        var parent: ContactsViewController

        init(_ contactsViewController: ContactsViewController) {
            self.parent = contactsViewController
        }

    }
}

And the SwiftUI file;

struct ContactsView: View {
    var body: some View {
        ContactsViewController()
    }
}

A reminder is I am calling the ContactsView inside of TabView in some other SwiftUI file. So I want show contacts in a SwiftUI View that is part of TabView. Any help would be really appreciated.

标签: swiftuiviewcontrollerswiftuicncontactpickercontactsui

解决方案


这里有两种解决方案:https ://stackoverflow.com/a/57621666/8818408

(1) 有效,但在显示联系人列表之前会闪现一个视图。对用户来说不是很好的体验。

(2)类作品。它还会在显示联系人列表之前闪烁一个视图。它以意想不到的透明度和意想不到的颜色显示在前缘、尾缘和底边。这些也许是可以解决的。

我刚刚开始尝试解决上面 #2 的问题。如果有人能提供一个完全有效的解决方案,那就太好了。#2 的屏幕截图


推荐阅读