Can't Access Contacts Sources on Device in iOS 6

后端 未结 3 1152
遥遥无期
遥遥无期 2021-02-13 10:39

This code worked OK on iOS 5.1 and also does work in the iPhone simulator with iOS 6. It fails silently on my iPhone 4 running iOS 6. The end result is that I cannot add a per

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-13 11:06

    If you got here from Google and you're using iOS's new CNContactStore framework, and getting these errors, read on:

    I thought it'd be cleaner to make my CNContactStore a member variable that was initialized with the class instance:

    class foo {
        var contactStore = CNContactStore()
    
        func findByIdentifier(identifier: String) -> CNContact {
            let contact = try self.contactStore.unifiedContactWithIdentifier(identifier...
            return contact
        }
    }
    

    After I called this about fifty times, it started erroring out with

    AB: Could not compile statement for query (ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties)

    I tried rate-limiting my calls, but that didn't help. It turned out that instantiating a new CNContactStore for every call had zero performance ramifications and completely solved the problem for me:

    class foo {
    
        func findByIdentifier(identifier: String) -> CNContact {
            let contactStore = CNContactStore()
            let contact = try contactStore.unifiedContactWithIdentifier(identifier...
            return contact
        }
    }
    

    Hope this helps!

提交回复
热议问题