2017-01-09 10 views
1

連絡先が変更されたときに関数を実行する必要があります。アプリケーションがアクティブな場合はNotificationCenterでこれをpostのナレーションにすることができます(既存の連絡先に新しい番号を追加すると機能することがあります)。連絡先(または連絡先)がアプリケーションの起動後に変更されたことをどのように知ることができますか?iOSでアプリケーションがアクティブでなかった場合、連絡先が変更されたことをどのように知ることができますか?

+0

ある私の仕事のために、以下の機能

@objc private func matchingContacts() { if isSuccessContactUploading { contactManager.matchingContacts(notMatch: { [weak self] in guard let _self = self else { return } debugPrint("matchingContacts != equals") _self.isSuccessContactUploading = false _self.syncContacts() }) } } 

を作った私は、OSが接触ストアに変更されたのを通知しますとは思いません。連絡先識別子(または変更があったことを確認する必要がある属性)をハッシュし、アプリケーションが起動され、必要なときに再度ハッシュしてみます。 – Sealos

+0

@Sealos私はそうするでしょう、私は突然、それ以上の解決策があると思っただけです。 – Alexander

答えて

1

私は、これらの関数は、ContactManager

func matchingContacts(notMatch: (() -> Void)?) { 
     getContacts { (contacts, error) in 
      if error == nil { 
       debugPrint("contacts count", contacts.count) 
       self.getContactsDictionaryFromCache(contacts, notMatch: { 
        notMatch?() 
       }) 
      } 
     } 
    } 

private func getContactsDictionaryFromCache(_ contacts: [CNContact], notMatch: (() -> Void)?) { 
     var isMatching = true 
     for contact in contacts { 
      let key = contact.identifier 

      do { 
       let cache = try Cache<NSDictionary>(name: "Contacts") 
       if let contactDictionary = cache[key] { 
        if !contactDictionary.isEqual(to: contact.dictionary) { 
         debugPrint("contactDictionary not matching") 
         isMatching = false 
        } 
       } else { 
        debugPrint("contactDictionary isn't here") 
        isMatching = false 
       } 
      } catch { 
       debugPrint(error.localizedDescription) 
       isMatching = false 
      } 
     } 

     if !isMatching { 
      notMatch?() 
     } 

     cacheContacts(contacts) 
    } 

private func cacheContacts(_ contacts: [CNContact]) { 
     for contact in contacts { 
      let contactDictionary = contact.dictionary as NSDictionary 
      let key = contact.identifier 

      do { 
       let cache = try Cache<NSDictionary>(name: "Contacts") 
       cache[key] = contactDictionary 
      } catch { 
       debugPrint(error.localizedDescription) 
      } 
     } 
    } 
関連する問題