Mac OS Sierra 破坏了 CNContact unifiedContact

CNContact unifiedContact broken with Mac OS Sierra

自从我切换到 Mac OS Sierra 和 XCode 8 后,使用 unifiedContactsMatchingPredicate:keysToFetch:error: 或 unifiedContactWithIdentifier:keysToFetch:error: 的代码不再 运行正确。

返回错误:错误域:CNErrorDomain 代码:200 并说明记录不存在,或记录更新失败

所有代码在 Mac OS 10.11 和 XCode 7 上工作正常,已授予对地址簿的读取权限,并且我正在寻找的 CNContact 确实存在。

我与 iOS 10 有相同的行为,我通过在 plist 文件中添加一个 NSContactsUsageDescription 键来解决这个问题(在 iOS 10 之前是可选的,但不再是)。我在我的 mac OS plist 文件中做了同样的事情,但没有成功。

知道发生了什么事以及在哪里搜索吗?

编辑:使用一些代码:

我写了 2 个新项目,一个在 Objective-C,一个在 Swift。两者都给出相同的结果。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application

CNContactStore  *contactStore   = [[CNContactStore alloc] init] ;
NSPredicate     *predicate      = [CNContact predicateForContactsMatchingName:@"TEST_CONTACT"] ;
NSError         *error          = nil ;

NSArray         *keys           = @[
                                    CNContactGivenNameKey,
                                    CNContactFamilyNameKey
                                    ] ;

NSArray         *contacts       = [contactStore unifiedContactsMatchingPredicate:predicate
                                                                     keysToFetch:keys
                                                                           error:&error] ;
NSLog(@"%d contacts found : %@",[contacts count],contacts) ;

}

func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Insert code here to initialize your application

    let contactStore    = CNContactStore()
    let predicate       = CNContact.predicateForContacts(matchingName: "TEST_CONTACT")


    let keys            = [CNContactGivenNameKey,CNContactFamilyNameKey]

    do
    {
        let contacts        = try contactStore.unifiedContacts(matching: predicate,
                                                               keysToFetch: keys as [CNKeyDescriptor])
        print(contacts)
    }
    catch
    {
        print("caught an error")
    }

}

控制台显示的结果:

2016-09-27 17:19:48.797029 TestCNContact.swift[13675:3502046] [core] __42-[ACAccountStore accountsWithAccountType:]_block_invoke_2 (208) "Error returned from daemon: <private>"

2016-09-27 17:19:48.798105 TestCNContact.swift[13675:3502046] [核心] __42-[ACAccountStore accountsWithAccountType:]_block_invoke_2 (208) "Error returned from daemon: " [] 2016-09-27 17:19:49.631876 TestCNContact.swift[13675:3502047] [错误] 警告:动态访问器在解析时未能找到实体 ABCDInfo 的 'uniqueId' 的 @属性 实现class 'ABCDInfo' 上的选择器 'uniqueId'。你记得在 @implementation 中声明它 @dynamic 或 @synthesized 吗? 2016-09-27 17:19:52.637627 TestCNContact.swift[13675:3502047] [错误] 警告:动态访问器无法在解析时为实体 ABCDAddressBookSource 找到 'serialNumber' 的 @属性 实现class 'ABCDAddressBookSource' 上的选择器 'serialNumber'。你记得在 @implementation 中声明它 @dynamic 或 @synthesized 吗? 2016-09-27 17:19:52.739108 TestCNContact.swift[13675:3502068] [错误] 警告:动态访问器无法在解析时为实体 ABCDAddressBookSource 找到 'uniqueId' 的 @属性 实现class 'ABCDAddressBookSource' 上的选择器 'uniqueId'。你记得在@implementation 中声明它@dynamic 或@synthesized 吗?

我找到了解决办法!!!

A​​pple 用以下声明回答了我的雷达: "Contacts now requires that anyone using it be codesigned with the uses-contacts entitlement."

所以我所做的是将我的应用程序沙盒化,并授予 "Contacts access" 权利。一切正常。

A​​pple 似乎将不再接受非沙盒应用访问通讯录(或位置或日历)。

我 运行 遇到了同样的问题,但沙盒不是一个选项,因为我们想通过 Sparkle 分发我们的应用程序。

在重新访问时,我重新阅读了声明:"Contacts now requires that anyone using it be codesigned with the uses-contacts entitlement.",并想知道如果我有一个列出了联系人但沙箱设置为 false 的权利文件会发生什么,并且它起作用了。

<dict>
        <key>com.apple.security.app-sandbox</key>
        <false/>
        <key>com.apple.security.personal-information.addressbook</key>
        <true/>
        <key>com.apple.security.personal-information.calendars</key>
        <true/>
</dict>

我使用 Activity Monitor 验证我的应用程序没有沙盒化,但仍然能够访问地址簿。