2017-02-22 4 views
1

연락처 식별자를 제공하는 연락처 일치를 얻지 못했습니다. 나는 연락을 되찾기를 원하고 그 다음에 그와 관련된 이미지를 사용하기를 원한다. 나는 매치를 얻고있다. 고맙습니다. 나는 데모에서 가져온이 코드는, 내가 그것을 :) 나는 dispatch_async 물건을 제거 해결 프로그램Swift 연락처 프레임 워크 사용, 일치하는 식별자를 사용하여 검색

import Contacts 

var contact = CNContact() 
var contactStore = CNContactStore() 

let foundContact = getContactFromID("94AAD3B1-E9E1-48C9-A796-F7EC1014230A") 

func getContactFromID(contactID: String) -> CNContact { 

    AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in 
     if accessGranted { 

      let predicate = CNContact.predicateForContactsWithIdentifiers([contactID]) 

      let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactImageDataAvailableKey] 

      var contacts = [CNContact]() 
      var message: String! 

      let contactsStore = AppDelegate.getAppDelegate().contactStore 

      do { 
       contacts = try contactsStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys) 

       if contacts.count == 0 { 
        message = "No contacts were found matching the given name." 
       } 
      } 
      catch { 
       message = "Unable to fetch contacts." 
      } 


      if message != nil { 
       dispatch_async(dispatch_get_main_queue(), {() -> Void in 
        Utility.showAlert(nil, message: message) 
       }) 
      } else { 

       dispatch_async(dispatch_get_main_queue(), {() -> Void in 

        self.contact = contacts[0] 

        print("self.contact: \(self.contact)") 

       }) 
      } 
     } 
    } 

    return self.contact 
} 
+0

나는 그것을 해결했다 :) 나는 dispatch_async 물건을 제거했다, 지금 작동한다 : 여기 고정 코드이다. – Mike

답변

2

에 좀 새로운 해요, 지금 작동 : 여기에 고정 된 코드입니다.

+0

firat 문자에서 시작으로 serch 접촉.이 유형 코드는 plz를 제공합니다. –