原先用系统自带的库,去实现读取联系人的功能,基本凑合够用,但是不够好用:
需要显示两个页面:
一个是通讯录,第二次是点击进去联系人详情,再去点击电话
才能返回:
点击号码后,才返回。
现在希望,只显示联系人的姓名和手机号,用户只点击一次,就返回
Alterplay/APAddressBook: Easy access to iOS address book
去写代码,提示需要操作通讯录:
最后用代码:
self.addressBook.fieldsMask = [APContactField.Default, APContactField.Thumbnail] self.addressBook.sortDescriptors = [NSSortDescriptor(key: "name.firstName", ascending: true), NSSortDescriptor(key: "name.lastName", ascending: true)] self.addressBook.filterBlock = { (contact: APContact) -> Bool in if let phones = contact.phones { return phones.count > 0 } return false } self.addressBook.loadContacts(self.loadContacts) func loadContacts(contacts: [APContact]?, error: NSError?){ gLog.verbose("contacts=\(contacts), error=\(error)") self.contactItemList.removeAll() if let contactList = contacts { for eachContact in contactList { gLog.verbose("eachContact=\(eachContact)") gLog.verbose("addresses=\(eachContact.addresses), birthday=\(eachContact.birthday) dates=\(eachContact.dates),emails=\(eachContact.emails),job=\(eachContact.job),linkedRecordIDs=\(eachContact.linkedRecordIDs),name=\(eachContact.name),note=\(eachContact.note),phones=\(eachContact.phones),recordDate=\(eachContact.recordDate),recordID=\(eachContact.recordID),relatedPersons=\(eachContact.relatedPersons),socialProfiles=\(eachContact.socialProfiles),source=\(eachContact.source),thumbnail=\(eachContact.thumbnail),websites=\(eachContact.websites)") let curContactItem = ContactItem() curContactItem.name = eachContact.name?.compositeName ?? "" curContactItem.phone = eachContact.phones?.first?.number ?? "" gLog.info("curContactItem=\(curContactItem)") self.contactItemList.append(curContactItem) } } gLog.info("got \(self.contactItemList.count) contacts") self.contactSectionItemList.removeAll() addContactItemToList(self.contactItemList) dispatchMain_async({ self.contactTableView.reloadData() }) } |
即可正常获取联系人信息: