iosswiftcncontactviewcontroller

iOS Contacts - save a single contact without permissions (or permissions UI)


CNAuthorizationStatus and the related documentation seems to suggest that you need permission to read or save to the CNContactStore. (And I've read most of the CNContact-based questions, and they are pretty consistent on that topic).

But I found an app that has a "Save Contact" button, and displays a contact in what looks like CNContactViewController, and when I pressed save, the app did save to contacts, but without any permissions dialog (and I never gave it permissions).

I uninstalled, re-installed and saved contact again, to confirm.

Has anyone done this before? I was on iOS 11.4.1.


Solution

  • I wrote an app that presents the Contact View for a new contact, and saves it to Contacts without:

    The has been published in the app store for sometime now, as "DropCard".

    The app also uses CNContactPickerViewController to select a single contact from the store, which interestingly says this:

    The app using contact picker view does not need access to the user’s contacts and the user will not be prompted for “grant permission” access. The app has access only to the user’s final selection.

    I conclude that similar-and-not-documented behavior is allowed for the creation of a single contact.

    NOTE: This behavior does not allow a rogue app to programmatically jam large amounts of trash data into the Contacts store, because you still need the user to click on a button to save each new contact.

    Here's the code I use for that portion

        let contact = CNMutableContact()
    
        let saveContactVC = CNContactViewController(forNewContact: contact)
    
        saveContactVC.contactStore = CNContactStore()
        saveContactVC.delegate = <your delegate here> as? CNContactViewControllerDelegate
        saveContactVC.allowsActions = false
    
        let navigationController = UINavigationController(rootViewController: saveContactVC)
        root.present(navigationController, animated: false)
        // NOTE: app *will* use right->left slide-over annimation...
        // then the 'animated: false' supresses the NavUI's top->new VC animation.