【问题标题】:Duplicates in accessing address book访问通讯录重复
【发布时间】:2016-02-27 10:37:45
【问题描述】:

出于某种原因,我不断收到许多我可以使用我的代码访问的联系人的重复项。有什么原因吗?

var error: Unmanaged<CFError>?
addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()


   if let people = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(self.addressBook, nil, ABPersonSortOrdering(kABPersonSortByFirstName)).takeRetainedValue() as? NSArray {
        for record in people {
            //var contactPerson: ABRecordRef = record
            var contactName: String = ABRecordCopyCompositeName(record).takeRetainedValue() as String


            var number = ""

            var phones: ABMultiValueRef = ABRecordCopyValue(record, kABPersonPhoneProperty).takeRetainedValue()

            for j in 0..<ABMultiValueGetCount(phones) {
                number = ABMultiValueCopyValueAtIndex(phones, j).takeRetainedValue() as! String
                break
            }

            if (number != "") {
                var newPerson = personInfo(name: contactName, number: number)
                allContacts.append(newPerson)
            }


           self.tableView.reloadData()
        }
    }

【问题讨论】:

    标签: swift


    【解决方案1】:

    James Richards 请使用联系人框架,而不是使用地址簿。

    首先你应该通过 Build Phases->Link Binary with Libraries->add(click +)->选择联系人框架来添加联系人框架

    import Contacts 
    

    然后

    let status = CNContactStore.authorizationStatusForEntityType(.Contacts)
    if status == .Denied || status == .Restricted {
            // user previously denied, so tell them to fix that in settings
            return
    }
    
    // open it
    
    let store = CNContactStore()
    store.requestAccessForEntityType(.Contacts) { granted, error in
            guard granted else {
                dispatch_async(dispatch_get_main_queue()) {
                    // user didn't grant authorization, so tell them to fix that in settings
                    print(error)
                }
                return
    }
    
    // get the contacts
    
    var contacts = [CNContact]()
    let request = CNContactFetchRequest(keysToFetch:[CNContactIdentifierKey, CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName)])
       do {
          try store.enumerateContactsWithFetchRequest(request) { contact, stop in
                    contacts.append(contact)
             }
       } 
       catch {
                print(error)
             }
    
            // do something with the contacts array (e.g. print the names)
    
            let formatter = CNContactFormatter()
            formatter.style = .FullName
            for contact in contacts {
                print(formatter.stringFromContact(contact))
            }
     }
    

    输出结果

    Optional("John Appleseed")
    Optional("Kate Bell")
    Optional("Anna Haro")
    Optional("Daniel Higgins Jr.")
    Optional("David Taylor")
    Optional("Hank M. Zakroff")
    

    Link 1

    Apple Document

    Contacts

    【讨论】:

    • 不幸的是,我还需要使该应用与 iOS 8 兼容
    猜你喜欢
    • 2011-10-16
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多