【问题标题】:iOS - CNContactStoreDidChangeNotification notification details discreapenciesiOS - CNContactStoreDidChangeNotification 通知细节差异
【发布时间】:2018-11-28 00:13:17
【问题描述】:

如何获取 iPhone 通讯录的最新变化

我即将在我的 iOS 应用程序中集成联系人框架,并希望在外部(由其他应用程序或用户)更改联系人(在联系人中添加/编辑字段)时收到通知。我找到了 CNContactStoreDidChangeNotification,想知道它是否会以有意义的形式带来联系人卡片中的确切更改和/或更改联系人的信息。

这里找不到

【问题讨论】:

    标签: ios objective-c iphone


    【解决方案1】:

    目前,CNContactStoreDidChangeNotification 没有给出更改。因此,如果您有一些缓存的联系人,则必须在通知到来时重新获取它们。

    -(void)registerForCNContactStoreDidChangeNotification {
       if (!self.hasRegisteredForNotifications) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(storeDidChange:) name:CNContactStoreDidChangeNotification  object:nil];
            self.hasRegisteredForNotifications = YES;
       }
    }
    
    - (void)deviceContactsDidChange:(NSNotification *)notification {
    // notification.userInfo can be nil
    
    // need to fetch all contacts with enumerateContactsWithFetchRequest to get all contacts or
    // refresh the cached contacts with unifiedContactsMatchingPredicate
    }
    

    Apple says

    如果您缓存了获取的联系人、群组或容器,则需要在发布 CNContactStoreDidChangeNotification 时重新获取这些对象(并释放旧的缓存对象)。

    【讨论】:

    • 谢谢@coconata。除了重新同步所有联系人之外,您是否知道任何解决方法?
    • unifiedContactsMatchingPredicate 允许获取唯一必要的联系人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多