【问题标题】:CNContactStore executeSaveRequest fails with CNErrorDomain Code=101 No Accessible Writable ContainersCNContactStore executeSaveRequest 失败,CNErrorDomain Code=101 No Accessible Writable Containers
【发布时间】:2019-01-17 09:33:04
【问题描述】:

我添加了一个代码,其中用户单击一个按钮并保存联系人,但现在我的代码不起作用,并且在执行 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=此应用程序无权访问任何可写联系人容器。} "

【问题讨论】:

  • 我试过你的代码,它对我来说工作正常。

标签: ios swift cncontactstore


【解决方案1】:

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

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

从 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

除此之外,您的代码在我看来还不错。

【讨论】:

  • 是的,它是一个企业应用程序,感谢您的回答。它真的帮助了我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 2018-05-24
相关资源
最近更新 更多