What is the Contacts Framework in Swift?
The Contacts framework is a built-in framework in Swift that allows you to access and manage your users’ contacts. You can use the framework to retrieve and display contact information, add new contacts, and modify existing contacts.
Using the Contacts Framework in a SwiftUI App
To use the Contacts framework in a SwiftUI app, you need to add the ContactsUI framework to your project and import it into your code. You can then use the CNContactPickerViewController to display a system-provided interface for selecting a contact.
Here’s an example of how to use the Contacts framework in a SwiftUI app:
import ContactsUI
import SwiftUI
struct ContactPickerView: UIViewControllerRepresentable {
@Environment(\.presentationMode) var presentationMode
@Binding var selectedContact: CNContact?
func makeUIViewController(context: Context) -> CNContactPickerViewController {
let controller = CNContactPickerViewController()
controller.delegate = context.coordinator
return controller
}
func updateUIViewController(_ uiViewController: CNContactPickerViewController, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, CNContactPickerDelegate {
var parent: ContactPickerView
init(_ parent: ContactPickerView) {
self.parent = parent
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
parent.selectedContact = contact
parent.presentationMode.wrappedValue.dismiss()
}
}
}
In this example, we’ve created a ContactPickerView that uses the UIViewControllerRepresentable protocol to wrap the CNContactPickerViewController. The view takes a binding to a selected contact and updates it when the user selects a contact from the picker. We've also added a coordinator class that handles the delegate methods for the picker.
Customizing the Contact Picker
You can customize the appearance and behavior of the contact picker to fit your app’s style and functionality. For example, you can add a title, filter out certain contact types, or change the color of the navigation bar.
Here’s an example of how to customize the contact picker:
func makeUIViewController(context: Context) -> CNContactPickerViewController {
let controller = CNContactPickerViewController()
controller.delegate = context.coordinator
controller.predicateForEnablingContact = NSPredicate(format: "emailAddresses.@count > 0")
controller.predicateForSelectionOfContact = NSPredicate(format: "emailAddresses.@count == 1")
controller.navigationBar.tintColor = .red
controller.navigationBar.topItem?.title = "Select a Contact"
return controller
}
In this example, we’ve added predicates to filter out contacts that don’t have email addresses and to automatically select a contact that has only one email address. We’ve also changed the color of the navigation bar to red and added a title.
Conclusion
The Contacts framework in Swift is a powerful tool that allows you to easily access and manage your users’ contact information. With just a few lines of code, you can create a customized contact picker that fits your app’s style and functionality.
Comments