如何获取我为AddressBook中的自定义属性设置的值以保留? [英] How can I get the values I set for custom properties in AddressBook to persist?

查看:176
本文介绍了如何获取我为AddressBook中的自定义属性设置的值以保留?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AddressBook中创建了名为Qref的自定义属性。我可以使用 [ABPerson属性] 检查它,并且它总是存在任何测试应用程序我写。

I have created a custom property in AddressBook named "Qref". I can check it's there using [ABPerson properties], and it's always there for any test app I write.

顺便说一下,您不能删除自定义属性,因为 [ABPerson removeProperties] 尚未实现。

By the way, you can't remove custom properties, because [ABPerson removeProperties] hasn't been implemented. Let me know if it ever is, because I need to remove one whose name I mistyped.

我用这个代码设置了一个属性值。

I set a property value with this code.

ABPerson *p = <person chosen from a PeoplePicker>;
NSError *e;
if (![p setValue: aString forProperty:@"Qref" error:&e]) {
  [NSAlert alertWithError:e]runModal;
}

(我从来没有看到警报,但有时会得到一堆错误消息在控制台。)
在这一点上,我可以离开PeoplePicker中的人,并返回找到正确设置的值。
如果我检查 [[ABAddressBook sharedAddressBook] hasUnsavedChanges] 结果是否,所以清楚地更改自定义属性值不计为一个更改,所以我强制(请建议更好的方法),然后执行

(I have never seen the alert yet, but sometimes get a heap of error messages in the console.) At this point I can navigate away from the person in the PeoplePicker and return to find the value correctly set. If I check [[ABAddressBook sharedAddressBook] hasUnsavedChanges] the result is NO, so clearly changing a custom property value doesn't count as a change, so I force a save by inserting dummy person (please suggest a better way), then executing

[[ABAddressBook sharedAddressBook] save];

假人正在运行时立即出现在AddressBook中,但是当我关闭我的应用程序并运行它,我发现我设置的值已经走了。
(MacOSX-Lion)

The dummy person appears immediately in AddressBook if it is running, so something's right. But when I close my app and run it again, I find the values I set have gone. (MacOSX-Lion)

推荐答案

我一直在咆哮错误的树。事实证明,我无法保存任何属性,无论它们是否是定制的。然后我想知道这是否与代码签名,权利或iCloud,这是我不可思议的丛林。
似乎你从PeoplePicker获得的人没有与任何地址簿相关联,因此 [[ABAddressBook sharedAddressBook] save] 将什么也不做。您必须从ABAddressBook实例获取您的人员。

I've been barking up the wrong trees. It's turning out that I couldn't save any properties, irrespective of whether they were custom ones or not. Then I wondered if it was something to do with code-signing, entitlements or iCloud, which is impenetrable jungle to me. It seems the person you get from a PeoplePicker isn't associated with any address book so [[ABAddressBook sharedAddressBook] save] will do nothing. You have to get your person from an ABAddressBook instance. Here's the skeleton of what works, without any error checking.

ABPerson *p = [[myPeoplePicker selectedRecords] objectAtIndex:0];
NSString *uid = p.uniqueId;
ABPerson *editablePerson = 
     (ABPerson*) [[ABAddressBook sharedAddressBook] recordForUniqueId:uid];
// Typecast because it returns an ABRecord. Can anyone improve?
[editablePerson setValue:@"ABCD" forProperty:@"Qref"]; // my custom property
[[ABAddressBook sharedAddressBook] save];

这篇关于如何获取我为AddressBook中的自定义属性设置的值以保留?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆