Guest User

Untitled

a guest
Nov 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. import ContactsUI
  2. import UIKit
  3.  
  4. class ViewController: UIViewController, CNContactPickerDelegate {
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7.  
  8. openAddressbook(vc: ViewController())
  9. }
  10.  
  11. //MARK:- CNContactPickerDelegate Method
  12.  
  13. var contactStore = CNContactStore()
  14.  
  15. func requestForAccess(completionHandler: @escaping (_ accessGranted: Bool) -> Void) {
  16. let authorizationStatus = CNContactStore.authorizationStatus(for: CNEntityType.contacts)
  17.  
  18. switch authorizationStatus {
  19. case .authorized:
  20. completionHandler(true)
  21.  
  22. case .denied, .notDetermined:
  23. self.contactStore.requestAccess(for: CNEntityType.contacts, completionHandler: { (access, accessError) -> Void in
  24. if access {
  25. completionHandler(access)
  26. }
  27. else {
  28. if authorizationStatus == CNAuthorizationStatus.denied {
  29. let message = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
  30. print(message);
  31. }
  32. }
  33. })
  34.  
  35. default:
  36. completionHandler(false)
  37. }
  38. }
  39.  
  40. func openAddressbook(vc: UIViewController){
  41. requestForAccess { (accessGranted) in
  42. if accessGranted == true{
  43. let cnPicker = CNContactPickerViewController()
  44. cnPicker.delegate = self
  45. self.present(cnPicker, animated: true, completion: nil)
  46. }
  47. }
  48. }
  49.  
  50. func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
  51. if contact.isKeyAvailable(CNContactPhoneNumbersKey){
  52. // handle the selected contact
  53. firstNameField.text = contact.givenName
  54. middleNameField.text = contact.middleName
  55. lastNameField.text = contact.familyName
  56.  
  57. var numberArray = [String]()
  58. for number in contact.phoneNumbers {
  59. let phoneNumber = number.value
  60. numberArray.append(phoneNumber.stringValue)
  61. }
  62. if numberArray.count > 0 {
  63. cellPhoneField.text = numberArray[0]
  64. if numberArray.count > 1 {
  65. officeNumberField.text = numberArray[1]
  66. if numberArray.count > 2 {
  67. homeNumberField.text = numberArray[2]
  68. }
  69. }
  70. }
  71. // Set the contact's home email address.
  72. var homeEmailAddress: String!
  73. for emailAddress in contact.emailAddresses {
  74. if emailAddress.label == CNLabelHome {
  75. homeEmailAddress = emailAddress.value as String
  76. break
  77. }
  78. }
  79.  
  80. if homeEmailAddress != nil {
  81. emailField.text = homeEmailAddress
  82. }
  83.  
  84. if contact.isKeyAvailable(CNContactPostalAddressesKey) {
  85. if let postalAddress = contact.postalAddresses.first?.value {
  86. // self.addressStreet.text = CNPostalAddressFormatter().string(from: postalAddress)
  87. self.addressStreet.text = postalAddress.street
  88. self.addressCity.text = postalAddress.city
  89. self.addressZip.text = postalAddress.postalCode
  90. self.addressState.text = postalAddress.state
  91. }
  92. }
  93.  
  94. companyCategoryField.text = "PersonalContact"
  95. companycategory = 3
  96.  
  97. }
  98. }
  99.  
  100. func contactPickerDidCancel(picker: CNContactPickerViewController) {
  101. print("Cancelled picking a contact")
  102. }
  103. }
Add Comment
Please, Sign In to add comment