构建 NSObject 以将 ABRecordRef 数据保存在本地

发布于 2024-12-01 02:42:37 字数 2309 浏览 4 评论 0原文

我想将 ABRecordRefContent 存储在我自己的应用程序中,独立于 AdressBook 中的条目。在交叉阅读 stackoverflow 和苹果开发者网站后,我发现这是最好的方法:

如果正确,则需要一个符合 NSCodingNSObject 子类,其所有值均为 < code>ABRecordRef 以及至少函数 initWithABRecordRefgetABRecordRefFromPeopleData (假设有人将他的类命名为 peopleData),其中initWithABRecordRef 会将 ABRecordRef 中的所有值(如果不为 null)填充到自己类的实例中,并且 getABRecordRefFromPeopleData 返回一个 ABRecordRef< /code> 不透明类型,数据存储在自己类的实例中。

我的问题:

我想知道是否有人已经这样做了,因为我可以想象,其他人之前也遇到过完全相同的问题。如果是这样,那么获得该课程将是非常棒的,如果不是,我将自己尝试一下,如果需要的话,我将把结果加载到这里。 也许您甚至知道更好的方法?

如果您确实实施了解决方案,请分享。如果您有同样的需求,我很乐意一起解决。

编辑:

我现在开始研究这件事,(正如我所料)我遇到了一些不清楚的问题。 当谈到 kABStringPropertyType 值时,我的概念非常简单。小例子:

@implementation RealRecord
@synthesize firstName; //NSString

- (id)initWithRecordRef:(ABRecordRef)record{

    //Single valued entrys
    NSString *contactName = (NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
    firstName=contactName;
    [contactName release];
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    //single value entrys
    self.firstName=             [aDecoder decodeObjectForKey:@"firstName"];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:firstName forKey:@"firstName"];
}

- (ABRecordRef)returnABRecordRefFromRealRecord{
    ABRecordRef returnRecord =ABPersonCreate();
    ABRecordSetValue(returnRecord, kABPersonFirstNameProperty, firstName, NULL);
    return returnRecord;
}

这很好用。现在我不太清楚如何使用 kABMultiStringPropertyType 做同样的事情。我做了一个 NSMutableArray emailList 并继续这样:

- (id)initWithRecordRef:(ABRecordRef)record{

    //Multi valued entrys
ABMutableMultiValueRef email=ABRecordCopyValue(record, kABPersonEmailProperty);
for (int i = 0; i < ABMultiValueGetCount(email); i++) {
    NSString *anEmail = [(NSString*)ABMultiValueCopyValueAtIndex(email, i) autorelease];
    [emailList addObject:anEmail];
}
return self;
}

现在我不放心,怎么做其余的任务就多值。我可以像对字符串那样对 NSMutableArray 进行编码和解码吗?还是必须为 emailList 中的所有电子邮件设置一个密钥?

我该如何将这该死的东西放回到 returnRecord 中?

I want to store the Content of a ABRecordRef in my own Application independent of the entry in the AdressBook. After crossreading through stackoverflow and the apple developers site i found this to be the best way:

If got that right, one would need a NSCoding conform NSObject subclass with all values of ABRecordRef and at least the functions initWithABRecordRef and getABRecordRefFromPeopleData (assuming one names his class peopleData), where initWithABRecordRef would fill all values from an ABRecordRef (if not null) into an instance of the own class and getABRecordRefFromPeopleData returns an ABRecordRef opaque type with the Data stored in an instance of the own class.

My question to that:

I wonder if someone out there already did that, because I can imagine, other people came to the exact same problem before. If so, getting hands on that class would be aewsome, if not i am going to give it a try on my own and load the results up here if wanted.
Maybe you even know a better way to do that?

If you did implement a solution, please share it. If you need the same, I'd appreciate working that out together.

Edit:

I now started working on the thing and (as i expected) i ran into some unclear problems.
As it comes to kABStringPropertyType values my concept is pretty straight forward. Small example:

@implementation RealRecord
@synthesize firstName; //NSString

- (id)initWithRecordRef:(ABRecordRef)record{

    //Single valued entrys
    NSString *contactName = (NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
    firstName=contactName;
    [contactName release];
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    //single value entrys
    self.firstName=             [aDecoder decodeObjectForKey:@"firstName"];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:firstName forKey:@"firstName"];
}

- (ABRecordRef)returnABRecordRefFromRealRecord{
    ABRecordRef returnRecord =ABPersonCreate();
    ABRecordSetValue(returnRecord, kABPersonFirstNameProperty, firstName, NULL);
    return returnRecord;
}

This works fine. Now im not so clear with how to do the same thing with the kABMultiStringPropertyType.I made me a NSMutableArray emailList and went on like this:

- (id)initWithRecordRef:(ABRecordRef)record{

    //Multi valued entrys
ABMutableMultiValueRef email=ABRecordCopyValue(record, kABPersonEmailProperty);
for (int i = 0; i < ABMultiValueGetCount(email); i++) {
    NSString *anEmail = [(NSString*)ABMultiValueCopyValueAtIndex(email, i) autorelease];
    [emailList addObject:anEmail];
}
return self;
}

Now im not shure, how to do the rest of the tasks on the multi values. Can i just encode and decode the NSMutableArray like i did with the Strings or do i have to set a key for all of the emails in emailList?

And how do i get the damn thing back into the returnRecord?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人的歌 2024-12-08 02:42:37

研究使用 libsqlite3.dylib 并创建一个 SQL 数据库,该数据库将访问为所有属性生成的平面文件。

使用 sqlite3 库的示例 iOS 项目位于:

http://www.techotopia.com/index.php/ An_Example_SQLite_based_iOS_7_Application

&
有关使用 ABAddressbook 获取联系人多值属性的详细信息,请参见:

http://linuxsleuthing.blogspot.com/2012/10/addressing-ios6-address-book-and-sqlite.html

Look into using libsqlite3.dylib and creating a sql database that will access the flat files generated for all the properties.

Sample iOS project with sqlite3 library is here:

http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_7_Application

&
Detail on doing so with ABAddressbook for contact's multi-value properties here:

http://linuxsleuthing.blogspot.com/2012/10/addressing-ios6-address-book-and-sqlite.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文