0

我添加了一个代码,其中用户单击一个按钮并保存联系人,但现在我的代码不起作用,并且在执行 saveRequest 对象时失败。

private func checkContactsAccess(_ completionHandler: @escaping () -> Void) {
    switch CNContactStore.authorizationStatus(for: .contacts) {
    // Update our UI if the user has granted access to their Contacts
    case .authorized:
        completionHandler()

    // Prompt the user for access to Contacts if there is no definitive answer
    case .notDetermined :
        CNContactStore().requestAccess(for: .contacts) {granted, error in
            if granted {
                DispatchQueue.main.async {
                    completionHandler()
                }
            } else {
                print("not allowed")
            }
        }

    // Display a message if the user has denied or restricted access to Contacts
    case .denied,
         .restricted:
        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)
    }
}

// This method is called when the user has granted access to their address book data.
private func accessGrantedForContacts() {
    checkContactsAccess ({
            // Creating a mutable object to add to the contact
            let contact = CNMutableContact()

            contact.givenName = "John"
            contact.familyName = "Appleseed"

            contact.phoneNumbers = [CNLabeledValue(
                label:CNLabelPhoneNumberiPhone,
                value:CNPhoneNumber(stringValue:"(408) 555-0126"))]

            let homeAddress = CNMutablePostalAddress()
            homeAddress.street = "1 Infinite Loop"
            homeAddress.city = "Cupertino"
            homeAddress.state = "CA"
            homeAddress.postalCode = "95014"
            contact.postalAddresses = [CNLabeledValue(label:CNLabelHome, value:homeAddress)]

            let birthday = NSDateComponents()
            birthday.day = 1
            birthday.month = 4
            birthday.year = 1988  // You can omit the year value for a yearless birthday
            contact.birthday = birthday as DateComponents

            // Saving the newly created contact
            let store = CNContactStore()
            let saveRequest = CNSaveRequest()
            saveRequest.add(contact, toContainerWithIdentifier:nil)
        print(saveRequest)
            do {
                try store.execute(saveRequest)
            } catch {
                print(error)
            }
            print("Done")
            let alert = UIAlertController(title: "Saved",
                                          message: "Saved",
                                          preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        })
}

联系人应保存为“错误域=CNErrorDomain Code=101“无可访问可写容器”UserInfo={NSLocalizedDescription=无可访问可写容器,NSLocalizedFailureReason=此应用程序无权访问任何可写联系人容器。}”

4

1 回答 1

0

您是否在物理设备上的模拟器上进行测试?

您是否正在使用托管设备(通过企业帐户)?

从 iOS 11.3 开始,苹果声明:

Mobile Device Management
New Features

        Prevent unmanaged apps from accessing contacts in managed accounts. 

链接在这里https://developer.apple.com/library/archive/releasenotes/General/RN-iOS-11.3/index.html

除此之外,您的代码对我来说似乎很好。

于 2019-01-17T09:58:34.187 回答