来自 SwiftUI 中的通讯录应用程序的联系人列表 [英] List of Contacts from Contacts App in SwiftUI
问题描述
我正在尝试构建一个列出所有联系人的界面,就像具有相同 UI
的 Contacts
和 Phone
应用程序一样.到目前为止我尝试过的内容如下.基本上,我尝试使用 UIViewControllerRepresentable
从 ContactsUI
实现 CNContactPickerViewController
.但是我得到的是一个空白的白页.
struct ContactsViewController: UIViewControllerRepresentable {func makeUIViewController(context: UIViewControllerRepresentableContext) ->CNContactPickerViewController {让控制器 = CNContactPickerViewController()controller.delegate = context.coordinatorcontroller.displayedPropertyKeys = [CNContactGivenNameKey]返回控制器}func updateUIViewController(_ uiViewController: CNContactPickerViewController, context: UIViewControllerRepresentableContext) {打印(更新")}func makeCoordinator() ->协调员{返回协调员(自己)}类协调器:NSObject,CNContactPickerDelegate {var 父:ContactsViewController初始化(_contactsViewController:ContactsViewController){self.parent = contactsViewController}}}
和 SwiftUI 文件;
struct ContactsView:查看{var主体:一些视图{联系人视图控制器()}}
提醒一下,我正在其他一些 SwiftUI
文件中调用 TabView
内的 ContactsView
.所以我想在 SwiftUI
View
中显示联系人,它是 TabView
的一部分.任何帮助将非常感激.
这里有两种解决方案:
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.
There are two solutions here: https://stackoverflow.com/a/57621666/8818408
(1) works but flashes a view before showing the list of contacts. Not a good experience for the user.
(2) sort of works. It also flashes a view before showing the list of contacts. It shows with unexpected transparency and unexpected colors along leading, trailing, and bottom edges. These may be possible to sort out.
I'm just getting started trying to resolve issues with #2 above. If anyone can offer a solution that works completely, that would be great.
这篇关于来自 SwiftUI 中的通讯录应用程序的联系人列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!