swift - 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.
解决方案
这里有两种解决方案:https ://stackoverflow.com/a/57621666/8818408
(1) 有效,但在显示联系人列表之前会闪现一个视图。对用户来说不是很好的体验。
(2)类作品。它还会在显示联系人列表之前闪烁一个视图。它以意想不到的透明度和意想不到的颜色显示在前缘、尾缘和底边。这些也许是可以解决的。
推荐阅读
- python - 试图让程序在 id 之后添加一个字符串
- java - 如何在没有参数的情况下对特定行的 ArrayList 进行排序,以区分该特定元素中的第一个条目和第二个条目?
- python - 在列的行中选择有限的值
- javascript - 向页面添加漂亮的滚动条
- c - 如何在使用泛型指针时增加多个数据类型
- go - 不允许分配相同的类型
- ios - 具有两个视图的 SwiftUI Card Flip 动画,其中一个嵌入在 Stack 中
- php - 使用 laravel eloquent 仅向用户显示他尚未购买的产品
- python - 使用 Python 从文本中删除坏词
- jquery - 在 jquery 对象变量中为 `:checked` 添加规则