我只想通过电话号码获取联系人姓名。我尝试过这个,但速度太慢,CPU 速度超过 %120。
let contactStore = CNContactStore()
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]
var contacts = [CNContact]()
do {
try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest.init(keysToFetch: keys), usingBlock: { (contact, cursor) in
if (!contact.phoneNumbers.isEmpty) {
for phoneNumber in contact.phoneNumbers {
if let phoneNumberStruct = phoneNumber.value as? CNPhoneNumber {
do {
let libPhone = try util.parseWithPhoneCarrierRegion(phoneNumberStruct.stringValue)
let phoneToCompare = try util.getNationalSignificantNumber(libPhone)
if formattedPhone == phoneToCompare {
contacts.append(contact)
}
}catch {
print(error)
}
}
}
}
})
if contacts.count > 0 {
contactName = (contacts.first?.givenName)! + " " + (contacts.first?.familyName)!
print(contactName)
completionHandler(contactName)
}
}catch {
print(error)
}
另外,当我使用电话号码套件查找联系人时,其 CPU 增加并给出较晚的响应。
var result: [CNContact] = []
let nationalNumber = PhoneNumberKit().parseMultiple([phoneNumber])
let number = nationalNumber.first?.toNational()
print(number)
for contact in self.addressContacts {
if (!contact.phoneNumbers.isEmpty) {
let phoneNumberToCompareAgainst = number!.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
for phoneNumber in contact.phoneNumbers {
if let phoneNumberStruct = phoneNumber.value as? CNPhoneNumber {
let phoneNumberString = phoneNumberStruct.stringValue
let nationalContactNumber = PhoneNumberKit().parseMultiple([phoneNumberString])
let nationalContactNumberString = nationalContactNumber.first?.toNational()
if nationalContactNumberString == number {
result.append(contact)
}
}
}
}
}
return result
您的实现的问题是您在每次搜索时都会访问地址簿。
相反,如果您在第一次访问后将地址簿内容保存在内存中,则不会达到如此高的 CPU 使用率。
-
首先在控制器中保存一个惰性变量,该变量将保存地址簿内容:
lazy var contacts: [CNContact] = {
let contactStore = CNContactStore()
let keysToFetch = [
CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
CNContactEmailAddressesKey,
CNContactPhoneNumbersKey,
CNContactImageDataAvailableKey,
CNContactThumbnailImageDataKey]
// Get all the containers
var allContainers: [CNContainer] = []
do {
allContainers = try contactStore.containersMatchingPredicate(nil)
} catch {
print("Error fetching containers")
}
var results: [CNContact] = []
// Iterate all containers and append their contacts to our results array
for container in allContainers {
let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier)
do {
let containerResults = try contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: keysToFetch)
results.appendContentsOf(containerResults)
} catch {
print("Error fetching results for container")
}
}
return results
}()
- 当您查找具有特定电话号码的联系人时,遍历内存数组:
.
func searchForContactUsingPhoneNumber(phoneNumber: String) -> [CNContact] {
var result: [CNContact] = []
for contact in self.contacts {
if (!contact.phoneNumbers.isEmpty) {
let phoneNumberToCompareAgainst = phoneNumber.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
for phoneNumber in contact.phoneNumbers {
if let phoneNumberStruct = phoneNumber.value as? CNPhoneNumber {
let phoneNumberString = phoneNumberStruct.stringValue
let phoneNumberToCompare = phoneNumberString.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
if phoneNumberToCompare == phoneNumberToCompareAgainst {
result.append(contact)
}
}
}
}
}
return result
}
我用一个很大的地址簿测试了它,它工作顺利。
这是修补在一起的整个视图控制器以供参考。
import UIKit
import Contacts
class ViewController: UIViewController {
lazy var contacts: [CNContact] = {
let contactStore = CNContactStore()
let keysToFetch = [
CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
CNContactEmailAddressesKey,
CNContactPhoneNumbersKey,
CNContactImageDataAvailableKey,
CNContactThumbnailImageDataKey]
// Get all the containers
var allContainers: [CNContainer] = []
do {
allContainers = try contactStore.containersMatchingPredicate(nil)
} catch {
print("Error fetching containers")
}
var results: [CNContact] = []
// Iterate all containers and append their contacts to our results array
for container in allContainers {
let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier)
do {
let containerResults = try contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: keysToFetch)
results.appendContentsOf(containerResults)
} catch {
print("Error fetching results for container")
}
}
return results
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let contact = searchForContactUsingPhoneNumber("(555)564-8583")
print(contact)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func searchForContactUsingPhoneNumber(phoneNumber: String) -> [CNContact] {
var result: [CNContact] = []
for contact in self.contacts {
if (!contact.phoneNumbers.isEmpty) {
let phoneNumberToCompareAgainst = phoneNumber.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
for phoneNumber in contact.phoneNumbers {
if let phoneNumberStruct = phoneNumber.value as? CNPhoneNumber {
let phoneNumberString = phoneNumberStruct.stringValue
let phoneNumberToCompare = phoneNumberString.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
if phoneNumberToCompare == phoneNumberToCompareAgainst {
result.append(contact)
}
}
}
}
}
return result
}
}
I used 弗洛海的回答 https://stackoverflow.com/a/34095632/11077258对于惰性 var 部分。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)