2016-10-18 1 views
0

ANCS 외부 장치와 함께 작동하는 응용 프로그램에서 작업하고 있습니다. 일들을 짧게하기 위해, 장치는 SMS가 언제 들어 왔는지를 감지하고이를 앱에 알려줍니다. 그런 다음 앱은 웹 서비스 (Twilio)를 통해 SMS 발신자에게 메시지를 다시 보낼 수 있습니다.CNContactStore 한 문자열 내에서 2 개의 연락처 이름이 주어진 NSPredicate

SMS를 보낸 사람이 연락처에있는 경우 ANCS 장치는 전화 번호 자체가 아닌이 연락처의 이름 만 보게됩니다. 그래서 그것은이 읽을 수있는 이름 만 앱에 제공합니다. 하나 개의 이름은 연락처에있는 경우 (예 : "존") 다음 저장

let title = "John" 
let contactStore = CNContactStore() 
let predicate = CNContact.predicateForContacts(matchingName: title) 
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactMiddleNameKey, CNContactNicknameKey, CNContactOrganizationNameKey, CNContactPhoneNumbersKey] 
var contacts = [CNContact]() 
do { 
    contacts = try contactStore.unifiedContacts(matching: predicate, keysToFetch: keys as [CNKeyDescriptor]) 
    if contacts.count == 0 { 
     print("No contacts were found matching the given name.") 
    } 
    else 
    { 
     print("found \(contacts.count) matches !") 
     // from there find the best possible bet and the numbers for this contact 
    } 
} catch { 
    print("Unable to fetch contacts.") 
} 

하지만 가끔은 사람들이 여러 연락처, 연락처 및 전화 번호를 검색하는 것은 매우 간단과 같이이다 예를 들어 SMS가 들어올 때와 같은 다른 사람과 같은 사람은 "John or Dad"라고 말할 수 있습니다 - 주소록에있는 두 개의 연락처에 해당하기 때문에 ... (우리 모두는 우리의 주소록에서 중복). 때로는 최악의 경우 3 개 이상의 이름이있을 수 있습니다!

그 곳에서 내가 원하는 곳은 : 어떻게 그 연락처를 확인하기 위해 술어를 구별/만들 수 있습니까? 쉽지 않은 해결책은 "또는"단어에 이름을 쪼개는 것이지만, 이것이 코란, 독일어, sweedish 등의 단어라면 어떨까요? 그리고이 단어가 이름이 "John desk on the roof"인 단일 연락처에 속한다면? 최악의 경우 iOS는 지역화 된 분리 기호 단어 "or"를 제공합니까?

누구도 그런 문제에 직면 했습니까?

답변

0

내가 마지막으로 같이 할 관리 :

  • 첫째, 나는 각 언어에 대한 파일 TelephonyUtilities.lg에있는 단어를 사용, 애플 at this address에 의해 주어진 모든 transleted 또는 단어의 목록을 가지고 내가있을 때

  • 둘째, 나는

  • 3 (EN, ES 등)을 각각 원하는 언어의 키에 대한 모든 단어를 포함하는 PLIST 파일을 생성 연락처 이름, 나는 올바른 언어 문자열 givent 전화 선호 언어를 잡아, 그리고 그것을 분할하려고 ... 하나 이상의 결과가 발견되면, 이것은 아마도 두 연락처 이름이 동일한 연락처에 대한 것입니다, 그래서 나는 연락처를 가져와 공통 전화 번호를 찾습니다. 그리고 지금까지 꽤 잘 작동합니다.

몇 가지 샘플 코드 (SWIFT 3) 설명하기 :

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
<key>en</key> 
<string> or </string> 
<key>fr</key> 
<string> ou </string> 
<key>vi</key> 
<string> hoặc </string> 
<key>hr</key> 
<string> ili </string> 
<key>hu</key> 
<string> vagy </string> 
<key>id</key> 
<string> atau </string> 
<key>it</key> 
<string> o </string> 
<key>es</key> 
<string> o </string> 
<key>ar</key> 
<string> أو </string> 
<key>zh-CN</key> 
<string>还是</string> 
<key>zh-TW</key> 
<string> 或 </string> 
<key>sk</key> 
<string> alebo </string> 
<key>pt</key> 
<string> ou </string> 
<key>pl</key> 
<string> lub </string> 
<key>cs</key> 
<string> nebo </string> 
<key>ca</key> 
<string> o </string> 
<key>nl</key> 
<string> of </string> 
<key>fi</key> 
<string> tai </string> 
<key>da</key> 
<string> eller </string> 
<key>de</key> 
<string> oder </string> 
<key>el</key> 
<string> ή </string> 
<key>he</key> 
<string> או </string> 
<key>ja</key> 
<string>または</string> 
<key>ko</key> 
<string> 또는 </string> 
<key>ms</key> 
<string> atau </string> 
<key>no</key> 
<string> eller </string> 
<key>th</key> 
<string> หรือ </string> 
<key>uk</key> 
<string> чи </string> 
<key>tr</key> 
<string> veya </string> 
<key>ro</key> 
<string> sau </string> 
<key>ru</key> 
<string> или </string> 
<key>sv</key> 
<string> eller </string> 
</dict> 
</plist> 

이 얻기 다음 PLIST 파일 (이 도움이 될 수 있습니다 누구를 위해 완전한 하나)처럼 보이는 무엇

을 또는 단어 문자열 :

private func getOrWord() -> String 
{ 
    var orWord:String = "" 

    var languageCode:String = "" 
    if (NSLocale.current.languageCode != nil) { languageCode = NSLocale.current.languageCode! } 
    var countryCode:String = "" 
    if (NSLocale.current.regionCode != nil) { countryCode = NSLocale.current.regionCode! } 
    let bothCodes:String = "\(languageCode)-\(countryCode)" 

    var propertyListForamt = PropertyListSerialization.PropertyListFormat.xml //Format of the Property List. 
    var plistData: [String: AnyObject] = [:] 
    let plistPath: String? = Bundle.main.path(forResource: "separator", ofType: "plist")! //the path of the data 
    let plistXML = FileManager.default.contents(atPath: plistPath!)! 
    do { 
     plistData = try PropertyListSerialization.propertyList(from: plistXML, options: .mutableContainersAndLeaves, format: &propertyListForamt) as! [String:AnyObject] 

     if (languageCode != "") 
     { 
      if (plistData[languageCode] != nil) 
      { 
       orWord = plistData[languageCode] as! String 
      } 
      else if (plistData[bothCodes] != nil) 
      { 
       orWord = plistData[bothCodes] as! String 
      } 
     } 

    } catch { 
     print("Error reading plist: \(error), format: \(propertyListForamt)") 
    } 

    return orWord 
} 

마지막으로 가능한 연락처를 얻으십시오 :

private func searchForContacts(name:String) -> Array<CNContact> 
{ 
    var namesToSearch:Array<String> = [] 
    var contactsFound:Array<CNContact> = [] 

    let orWord:String = getOrWord() 

    if (orWord == "") 
    { 
     namesToSearch.append(name) 
    } 
    else 
    { 
     namesToSearch = name.components(separatedBy: orWord) 
    } 

    if (namesToSearch.count > 0) 
    { 
     let contactStore = CNContactStore() 

     for nameSearch in namesToSearch 
     { 
      let predicateName = CNContact.predicateForContacts(matchingName: nameSearch) 

      let keys = [CNContactPhoneNumbersKey] 
      var contacts = [CNContact]() 
      do { 
       contacts = try contactStore.unifiedContacts(matching: predicateName, keysToFetch: keys as [CNKeyDescriptor]) 
       if (contacts.count > 0) 
       { 
        // keep only the ones that have at least 1 phone number 
        for contact in contacts 
        { 
         if (contact.phoneNumbers.count > 0) 
         { 
          contactsFound.append(contact) 
         } 
        } 

       } 
      } catch { 
       // not possible to fetch 
      } 
     } 
    } 

    return contactsFound 
} 
+0

이봐, 나에게는 같은 문제가 있지만 내 자신 만의 맞춤 연락처 UI를 만들려고하고있다. 섹션별로 연락처 배열을 가져 오려고합니다. 즉, 섹션 A, B, C 등의 모든 연락처를 가져 오는 것입니다.저는 테이블 뷰에 그것들을 표시하려고 노력하고 있습니다 만, 저는 다른 ASCII 및 ASCII가 아닌 문자들도 고려해야 할 필요가 있기 때문에 복잡합니다! 따라서 전체 목록 대신 섹션별로 가져 오는 것이 더 쉬울지라도 –