How to fetch all contacts record in iOS 9 using Contacts Framework

前端 未结 20 1655
[愿得一人]
[愿得一人] 2020-11-28 01:52

Most part of AddressBook framework is deprecated in iOS 9. In the new Contacts Framework documentation only shows how to fetch records matches a NSPredicate, bu

20条回答
  •  猫巷女王i
    2020-11-28 02:15

    @rocolitis 's answer in swift! His answer is the most correct way of doing this according to Apple's documentation.

    let contactStore = CNContactStore()
    let keys = [CNContactPhoneNumbersKey, CNContactFamilyNameKey, CNContactGivenNameKey, CNContactNicknameKey] as [CNKeyDescriptor]
    let request = CNContactFetchRequest(keysToFetch: keys)
    
    try? contactStore.enumerateContacts(with: request) { (contact, error) in
    
        // Do something with contact
    
    }
    

    You should probably check your access to your contacts first!

    let authorization = CNContactStore.authorizationStatus(for: CNEntityType.contacts)
    
    switch authorization {
    case .authorized: break
    case .denied: break
    case .restricted: break
    case .notDetermined: break
    }
    

提交回复
热议问题