//MARK: Contacts

    func checkContactsStatus() {
        let status = CNContactStore.authorizationStatus(for: .contacts)
        if status == .denied || status == .restricted {
            presentSettingsActionSheet()
            return
        }
    }

   //Ask for Contact Permissions
    func presentSettingsActionSheet() {
        let alert = UIAlertController(title: "Permission to Contacts", message: "This app needs access to contacts in order to ...", preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Go to Settings", style: .default) { _ in
            let url = URL(string: UIApplicationOpenSettingsURLString)!
            UIApplication.shared.open(url)
        })
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
        present(alert, animated: true)
    }

    func contactPicker(_ picker: CNContactPickerViewController,
                       didSelect contactProperty: CNContactProperty) {

    }

    func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
        // You can fetch selected name and number in the following way

        // user name
        let userName:String = contact.givenName
        let surName:String = contact.familyName
        let fullName:String = userName + " " + surName
        print(fullName)
        contactTextField.text = fullName

//         user phone number
//        let userPhoneNumbers:[CNLabeledValue<CNPhoneNumber>] = contact.phoneNumbers
//        let firstPhoneNumber:CNPhoneNumber = userPhoneNumbers[0].value

        // user phone number string
//        let primaryPhoneNumberStr:String = firstPhoneNumber.stringValue
//        print(primaryPhoneNumberStr)
    }

    func contactPickerDidCancel(_ picker: CNContactPickerViewController) {

    }

    func chooseContact() {
        let contactPicker = CNContactPickerViewController()
        contactPicker.delegate = self
        contactPicker.displayedPropertyKeys =
            [CNContactGivenNameKey
                , CNContactPhoneNumbersKey]
        self.present(contactPicker, animated: true, completion: nil)
    }

Info.plist

<key>NSContactsUsageDescription</key>
<string>This app requires contacts access to function properly.</string>
import Foundation
import CoreData

class CoreDataStack {
    var container: NSPersistentContainer {
        let container = NSPersistentContainer(name: "todos")
        container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        container.loadPersistentStores { (description, error) in
            guard error == nil else {
                print("Error: \(error!)")
                return
            }
        }
        return container
    }

    var managedContext: NSManagedObjectContext {
        return container.viewContext
    }
}

If embedded in Navigation Controller:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if let rootViewController = window?.rootViewController as? UINavigationController {
    if let viewController = rootViewController.viewControllers.first as? ViewController {
        let url = alert["imgurl"] as? NSString
        viewController.loadImage(url as String)
    }
}
}

If not embedded in Navigation Controller:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if let rootViewController = window?.rootViewController as? ViewController {
        rootViewController.loadImage()
    }
}
let alert = UIAlertController(title: "Title", message: nil, preferredStyle: .alert)
alert.addTextField { (textField) in
        textField.placeholder = "Name"
    }

let action = UIAlertAction(title: "Save", style: .default) { (_) in 
}

let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action: UIAlertAction) in
    }

alert.addAction(action)
    alert.addAction(cancel)

present(alert, animated: true, completion: nil)

In an alert view:

 let action = UIAlertAction(title: "Update", style: .default) { (_) in

        let name = alert.textFields!.first!.text!
        let age = alert.textFields!.last!.text!


        selectedItem.setValue(name, forKey: "name")
        selectedItem.setValue(Int16(age), forKey: "age")

        do{
            try persistenceService.context.save()
        } catch {
            print("Error saving")
        }

        self.tableView.reloadData()
    }

In a tableview:

        let itemToDelete = people[indexPath.row]

    print(itemToDelete)

    if (editingStyle == UITableViewCellEditingStyle.delete) {
        // handle delete (by removing the data from your array and updating the tableview)

        persistenceService.context.delete(itemToDelete)
        self.people.remove(at: indexPath.row)

        do {
            try persistenceService.context.save()
        } catch let error as NSError {
            print("Error While Deleting: \(error.userInfo)")
        }


    }

In an alert view:

 let action = UIAlertAction(title: "Save", style: .default) { (_) in
            let name = alert.textFields!.first!.text!
            let age = alert.textFields!.last!.text!

//            fill container
            let person = Person(context: persistenceService.context)
            person.name = name
            person.age = Int16(age)!

//            save container
            persistenceService.saveContext()
            self.people.append(person)

        self.tableView.reloadData()

    }