2017-03-12 14 views
-3

Contacts framework을 사용하여 연락처를 가져올 수있는 방법이 있습니까? 속성이 없습니까?Swift에서 "John"이라는 이름이 아닌 연락처를 가져 오는 방법 3

예 :

myContactArray = unifiedContactsNotCalled("John") 

PS : 그 라인은 내가 일치하지 않는 사람을 찾는 방법을 간략하게 설명하기 전에,이 설명을 목적으로

+0

FWIW, 나는이 생각하지 않는다 "너무 광범위." 연락처 프레임 워크 (그리고 그것이 수반하는 어려움)를 알고 있다면이 질문은 분명합니다. – Rob

답변

2

을 단지 서빙 제안의 실제 코드처럼 아무것도 알고 이름, 어떻게하는 사람들을 찾아내는 지 간략히 보겠습니다. 즉, 당신은 술어를 사용하십시오 : (. - try - 물론, 당신이 do에 그 포장 줄 어떤 오류가 당신이 원하는 패턴을 처리 catch 구조, 또는)

let predicate = CNContact.predicateForContacts(matchingName: searchString) 
let matches = try store.unifiedContacts(matching: predicate, keysToFetch: [CNContactFormatter.descriptorForRequiredKeys(for: .fullName)]) // use whatever keys you want 

불행하게도, 당신을 연락처 프레임 워크에서 사용자 정의 술어를 사용할 수 없으며 사전 정의 된 술어 CNContact 만 사용할 수 있습니다. 당신은 이름이 "존"을 포함하지 않는 연락처를 찾으려면 따라서, 수동 enumerateContacts(with:) 그에서 결과를 구축해야 :

let formatter = CNContactFormatter() 
formatter.style = .fullName 

let request = CNContactFetchRequest(keysToFetch: [CNContactFormatter.descriptorForRequiredKeys(for: .fullName)]) // include whatever other keys you may need 

// find those contacts that do not contain the search string 

var matches = [CNContact]() 
try store.enumerateContacts(with: request) { contact, stop in 
    if !(formatter.string(from: contact)?.localizedCaseInsensitiveContains(searchString) ?? false) { 
     matches.append(contact) 
    } 
}