首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CNContactStore executeSaveRequest失败,并显示CNErrorDomain Code=101 No Accessible Writable Containers (没有可访问的可写容器

CNContactStore executeSaveRequest失败,并显示CNErrorDomain Code=101 No Accessible Writable Containers (没有可访问的可写容器
EN

Stack Overflow用户
提问于 2019-01-17 17:33:05
回答 1查看 219关注 0票数 0

我已经添加了一段代码,其中用户单击一个按钮,联系人将被保存,但现在我的代码不工作,它在执行saveRequest对象时失败。

代码语言:javascript
复制
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=This应用程序无权访问任何可写联系人容器。}”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-17 17:58:34

您是否在物理设备上测试Simulator on?

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

从iOS 11.3开始,苹果声明:

代码语言:javascript
复制
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

除此之外,我觉得你的代码没问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54232819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档