Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ContactViewController.swift
- // ___
- //
- // Created by Kurtulus Ahmet on 4.01.2019.
- // Copyright © 2019 ___. All rights reserved.
- //
- import Contacts
- import ContactsUI
- import UIKit
- class ContactViewController: UIViewController, CNContactPickerDelegate, CNContactViewControllerDelegate {
- private var store: CNContactStore!
- override func viewDidLoad() {
- super.viewDidLoad()
- store = CNContactStore()
- checkContactsAccess()
- }
- //Rehber erişim iznine erişmek istediğimizde kullanıcıya bir alert gösteriyoruz.
- private func showPrivacyWarningAlert() {
- //TODO: Trello kaydı oluşturuldu.
- let alert = UIAlertController(title: "Privacy Warning!",
- message: "Permission was not granted for Contacts.",
- preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
- self.present(alert, animated: true, completion: nil)
- }
- private func checkContactsAccess() {
- switch CNContactStore.authorizationStatus(for: .contacts) {
- // Kullanıcıya rehberine erişmek istediğimizi bildirmemiz gerekli.
- case .notDetermined :
- self.requestContactsAccess()
- // Eğer kullanıcı daha önceden rehber erişimini reddederse tekrardan izin soruyoruz.
- case .denied,
- .restricted:
- showPrivacyWarningAlert()
- case .authorized:
- break
- }
- }
- private func requestContactsAccess() {
- store.requestAccess(for: .contacts) {granted, _ in
- if granted {
- return
- } else {
- self.showPrivacyWarningAlert()
- }
- }
- }
- // MARK: Show all contacts
- // Bütün rehberin listelenmesi sağlanıyor.
- private func showContactPickerController() {
- let picker = CNContactPickerViewController()
- picker.delegate = self
- // Sadece telefon numarası ve kişi adı, emailve doğum tarihi gösteriliyor.
- let displayedItems = [CNContactPhoneNumbersKey, CNContactEmailAddressesKey, CNContactBirthdayKey]
- picker.displayedPropertyKeys = displayedItems
- self.present(picker, animated: true, completion: nil)
- }
- @IBAction func openContactAction(_ sender: UIButton) {
- self.showContactPickerController()
- }
- // MARK: CNContactPickerDelegate methods
- // Seçilen kişinin bilgileri alınıyor.
- func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
- let contact = contactProperty.contact
- let contactName = CNContactFormatter.string(from: contact, style: .fullName) ?? ""
- let message = "Seçilen: \(contactName)"
- DispatchQueue.main.async {
- let alert = UIAlertController(title: "Sonuç",
- message: message,
- preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "Tamam", style: .default, handler: nil))
- self.present(alert, animated: true, completion: nil)
- }
- }
- // Seçici kullanıcı tarafından iptal edildiğinde ek iş yapmak istiyorsanız bunu uygulayın.
- func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
- picker.dismiss(animated: true, completion: {})
- }
- }
Add Comment
Please, Sign In to add comment