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

前端 未结 20 1523
[愿得一人]
[愿得一人] 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条回答
  • 2020-11-28 02:25

    Just wanted to share this versions of Swift 4

    info.plist:

    <key>NSContactsUsageDescription</key>
        <string>$(PRODUCT_NAME) requires to access your contacts ...</string>
    

    module:

    import Contacts
    

    code:

    func fetchContacts(completion: @escaping (_ result: [CNContact]) -> Void){
            DispatchQueue.main.async {
                var results = [CNContact]()
                let keys = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactMiddleNameKey,CNContactEmailAddressesKey,CNContactPhoneNumbersKey] as [CNKeyDescriptor]
                let fetchRequest = CNContactFetchRequest(keysToFetch: keys)
                fetchRequest.sortOrder = .userDefault
                let store = CNContactStore()
                store.requestAccess(for: .contacts, completionHandler: {(grant,error) in
                    if grant{
                        do {
                            try store.enumerateContacts(with: fetchRequest, usingBlock: { (contact, stop) -> Void in
                                results.append(contact)
                            })
                        }
                        catch let error {
                            print(error.localizedDescription)
                        }
                        completion(results)
                    }else{
                        print("Error \(error?.localizedDescription ?? "")")
                    }
                })
            }
        }
    

    Usage:

    fetchContacts(completion: {contacts in
                contacts.forEach({print("Name: \($0.givenName), number: \($0.phoneNumbers.first?.value.stringValue ?? "nil")")})
    

    You need to describe the usage info in info.plist first. I have added a check to determine user has granted the access to contacts then defined the keys (the values needed to be fetched). As said in one of the earlier answers that it's a time consuming process so I have added DispatchQueue for background processing and completion handler for returning the contacts array back to caller.

    0 讨论(0)
  • 2020-11-28 02:26

    Swift 4.2 . Fetch contact numbers with image

    info.plist file data
    <key>NSContactsUsageDescription</key>
    <string>$(PRODUCT_NAME) requires to access your contacts ...</string>
    
    
    
    //MARK:- Fetch All Contacts of Phone
    func fetchContacts(completion: @escaping (_ result: [CNContact]) -> Void){
        DispatchQueue.main.async {
            var results = [CNContact]()
            let keys = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactMiddleNameKey,CNContactEmailAddressesKey,CNContactPhoneNumbersKey,CNContactThumbnailImageDataKey] as [CNKeyDescriptor]
            let fetchRequest = CNContactFetchRequest(keysToFetch: keys)
            fetchRequest.sortOrder = .userDefault
            let store = CNContactStore()
            store.requestAccess(for: .contacts, completionHandler: {(grant,error) in
                if grant{
                    do {
                        try store.enumerateContacts(with: fetchRequest, usingBlock: { (contact, stop) -> Void in
                            results.append(contact)
                        })
                    }
                    catch let error {
                        print(error.localizedDescription)
                    }
                    completion(results)
                }else{
                    print("Error \(error?.localizedDescription ?? "")")
                }
            })
        }
    }
    

    }

    Function Calling in Did Load Method

    var arrpic = NSMutableArray()
    
     override func viewDidLoad() {
        super.viewDidLoad()
    
        fetchContacts(completion: {contacts in
            contacts.forEach({print("Name: \($0.givenName), number: \($0.phoneNumbers.first?.value.stringValue ?? "nil")")
                self.arrfname.append("\($0.givenName)")
                self.arrlname.append("\($0.familyName)")
                self.arrnumber.append("\($0.phoneNumbers.first?.value.stringValue ?? "nil")")
                var img = UIImage()
                if $0.thumbnailImageData != nil
                {
                    img = UIImage.init(data: $0.thumbnailImageData!)!
                    self.arrpic.add(img)
                }
                else
                {
                    self.arrpic.add("")
                }
            })
            if contacts.count > 0
            {
                self.tablev.reloadData()
            }
        })
    }
    
    0 讨论(0)
提交回复
热议问题