2017-02-22 3 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

にちょっと新たなんだ、今作品:ここに固定されたコードです。

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." 
       } 

       self.contact = contacts[0] 
      } 
     } 
     return self.contact 
    } 
+0

firatキャラクタからstartchへのserch接触。このタイプコードはplzを提供します。 –

関連する問題