ios - CNContactStoreexecuteSaveRequest 失败,CNErrorDomain Code=101 No Accessible Writable Containers

标签 ios swift cncontactstore

我添加了一段代码,其中用户单击按钮,联系人将被保存,但现在我的代码不起作用,并且在执行 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)
        })
}

应将联系人保存为错误,而不是错误“Error Domain=CNErrorDomain Code=101“No Accessible Writable Containers”UserInfo={NSLocalizedDescription=No Accessible Writable Containers, NSLocalizedFailureReason=此应用程序无权访问任何可写联系人容器。} ”

最佳答案

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

您是否在托管设备上工作(通过企业帐户)?

从 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

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

关于ios - CNContactStoreexecuteSaveRequest 失败,CNErrorDomain Code=101 No Accessible Writable Containers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54232819/

相关文章:

ios - 我可以将我的对象保存在内存中直到应用程序退出吗?

ios - 无法在 iOS 12.4.1 (iPhone XR) 中使用 CNContactStore 获取联系人。即使在 13 beta 或 12.4.1 以下也能正常工作

iOS - 如何使用 SOAP 发送附件?

ios swift 显示/隐藏textView单元格中的元素并清除其空间

ios - 更改 UIImagePickerController 的 TintColor

ios - AdMob iOS 横幅偏移问题

ios - Pan 手势识别器 : Location of touch crashes once the pan gesture is released

swift - 在 Swift 中访问多维数组的最有效方法?

ios - 从远程播放 MKV 视频

ios - CNContacts -- 地址行 2