Guest User

Untitled

a guest
Jan 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. //
  2. // ContactViewController.swift
  3. // ___
  4. //
  5. // Created by Kurtulus Ahmet on 4.01.2019.
  6. // Copyright © 2019 ___. All rights reserved.
  7. //
  8. import Contacts
  9. import ContactsUI
  10. import UIKit
  11.  
  12. class ContactViewController: UIViewController, CNContactPickerDelegate, CNContactViewControllerDelegate {
  13. private var store: CNContactStore!
  14.  
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. store = CNContactStore()
  18. checkContactsAccess()
  19. }
  20.  
  21. //Rehber erişim iznine erişmek istediğimizde kullanıcıya bir alert gösteriyoruz.
  22. private func showPrivacyWarningAlert() {
  23. //TODO: Trello kaydı oluşturuldu.
  24. let alert = UIAlertController(title: "Privacy Warning!",
  25. message: "Permission was not granted for Contacts.",
  26. preferredStyle: .alert)
  27. alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
  28. self.present(alert, animated: true, completion: nil)
  29. }
  30.  
  31. private func checkContactsAccess() {
  32. switch CNContactStore.authorizationStatus(for: .contacts) {
  33. // Kullanıcıya rehberine erişmek istediğimizi bildirmemiz gerekli.
  34. case .notDetermined :
  35. self.requestContactsAccess()
  36. // Eğer kullanıcı daha önceden rehber erişimini reddederse tekrardan izin soruyoruz.
  37. case .denied,
  38. .restricted:
  39. showPrivacyWarningAlert()
  40. case .authorized:
  41. break
  42. }
  43. }
  44.  
  45. private func requestContactsAccess() {
  46. store.requestAccess(for: .contacts) {granted, _ in
  47. if granted {
  48. return
  49. } else {
  50. self.showPrivacyWarningAlert()
  51. }
  52. }
  53. }
  54.  
  55. // MARK: Show all contacts
  56. // Bütün rehberin listelenmesi sağlanıyor.
  57. private func showContactPickerController() {
  58. let picker = CNContactPickerViewController()
  59. picker.delegate = self
  60. // Sadece telefon numarası ve kişi adı, emailve doğum tarihi gösteriliyor.
  61. let displayedItems = [CNContactPhoneNumbersKey, CNContactEmailAddressesKey, CNContactBirthdayKey]
  62. picker.displayedPropertyKeys = displayedItems
  63. self.present(picker, animated: true, completion: nil)
  64. }
  65.  
  66. @IBAction func openContactAction(_ sender: UIButton) {
  67. self.showContactPickerController()
  68. }
  69.  
  70. // MARK: CNContactPickerDelegate methods
  71. // Seçilen kişinin bilgileri alınıyor.
  72. func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
  73. let contact = contactProperty.contact
  74. let contactName = CNContactFormatter.string(from: contact, style: .fullName) ?? ""
  75. let message = "Seçilen: \(contactName)"
  76. DispatchQueue.main.async {
  77. let alert = UIAlertController(title: "Sonuç",
  78. message: message,
  79. preferredStyle: .alert)
  80. alert.addAction(UIAlertAction(title: "Tamam", style: .default, handler: nil))
  81. self.present(alert, animated: true, completion: nil)
  82. }
  83. }
  84.  
  85. // Seçici kullanıcı tarafından iptal edildiğinde ek iş yapmak istiyorsanız bunu uygulayın.
  86. func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
  87. picker.dismiss(animated: true, completion: {})
  88. }
  89. }
Add Comment
Please, Sign In to add comment