Last active
May 7, 2021 11:12
-
-
Save scriptpapi/fee46844a7cd53aa38af2978d68bae66 to your computer and use it in GitHub Desktop.
Contact Picker for SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Contacts | |
import Combine | |
import Foundation | |
import ContactsUI | |
struct ContactPicker: UIViewControllerRepresentable { | |
typealias UIViewControllerType = EmbeddedContactPickerViewController | |
@Environment(\.presentationMode) var presentationMode | |
@Binding var selectedContact: CNContact? | |
func makeCoordinator() -> Coordinator { | |
return Coordinator(self) | |
} | |
final class Coordinator: NSObject, EmbeddedContactPickerViewControllerDelegate { | |
let parent: ContactPicker | |
init(_ parent: ContactPicker) { | |
self.parent = parent | |
} | |
func embeddedContactPickerViewController(_ viewController: EmbeddedContactPickerViewController, didSelect contact: CNContact) { | |
parent.selectedContact = contact | |
viewController.dismiss(animated: true, completion: nil) | |
} | |
func embeddedContactPickerViewControllerDidCancel(_ viewController: EmbeddedContactPickerViewController) { | |
print("Cancelled") | |
} | |
} | |
func makeUIViewController(context: UIViewControllerRepresentableContext<ContactPicker>) -> ContactPicker.UIViewControllerType { | |
let result = ContactPicker.UIViewControllerType() | |
result.delegate = context.coordinator | |
return result | |
} | |
func updateUIViewController(_ uiViewController: ContactPicker.UIViewControllerType, context: UIViewControllerRepresentableContext<ContactPicker>) { } | |
} | |
protocol EmbeddedContactPickerViewControllerDelegate: class { | |
func embeddedContactPickerViewControllerDidCancel(_ viewController: EmbeddedContactPickerViewController) | |
func embeddedContactPickerViewController(_ viewController: EmbeddedContactPickerViewController, didSelect contact: CNContact) | |
} | |
class EmbeddedContactPickerViewController: UIViewController, CNContactPickerDelegate { | |
weak var delegate: EmbeddedContactPickerViewControllerDelegate? | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
self.open(animated: animated) | |
} | |
private func open(animated: Bool) { | |
let viewController = CNContactPickerViewController() | |
viewController.delegate = self | |
self.present(viewController, animated: false) | |
} | |
func contactPickerDidCancel(_ picker: CNContactPickerViewController) { | |
self.dismiss(animated: false) { | |
self.delegate?.embeddedContactPickerViewControllerDidCancel(self) | |
} | |
} | |
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) { | |
self.dismiss(animated: false) { | |
self.delegate?.embeddedContactPickerViewController(self, didSelect: contact) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment