如何使用 ABUnknownPersonViewController 与最初输入的生成数据?

发布于 2024-11-01 17:08:49 字数 404 浏览 1 评论 0原文

这是一个非常具体的案例。我相信有人已经在某个地方解决了这个问题,但对我来说找到它并不容易。

情况:

1 )一个对象将返回名称地址1、地址2、电话的 NSString 对象:

[anObject name];
[anObject address1];
[anObject address2];
[anObject name];

2)我想使用这些对象来准备带有初始输入值的 ABUnknownPersonViewController ,因此用户在将它们保存到地址簿之前无需输入它们。

我查看了iOS文档并通过Google和StackOverflow进行搜索,无法找到这个简单情况的正确答案。

有人可以指导我吗?

This is very specific case. I believe someone had already solved this somewhere, but it's not easy for me to find it.

The situation:

1 ) an object will return NSString objects for name address1, address2, phone:

[anObject name];
[anObject address1];
[anObject address2];
[anObject name];

2 ) I would like to use these objects to prepare ABUnknownPersonViewController with initially entered values, so the user will not have to enter them before saving them in Address Book.

I have looked at iOS documents and searched through Google and StackOverflow, can't find the right answer for this simple situation.

Can anyone guide me on this?

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

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

发布评论

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

评论(1

蓝眼泪 2024-11-08 17:08:49

找到了答案:它在 iOS 开发人员库中有很好的记录:
http://developer.apple.com/library/ios/#samplecode/QuickContacts/Listings/Classes_QuickContactsViewController_m.html#//apple_ref/doc/uid/DTS40009475-Classes_QuickContactsViewController_m-DontLinkElementID_6

这里是我实现的示例代码将 ABPersonRecordRef 作为对象返回。我遇到的错误与返回 ABPersonRecordRef 对象后不保留它有关。

- (id)personRecordUsingModelObj:(id)modelObj {
    ABRecordRef aContact = ABPersonCreate();
    CFErrorRef anError = NULL;

    NSString *name = [NSString stringWithFormat:@"%@", [modelObj name]];
    ABRecordSetValue(aContact, kABPersonOrganizationProperty, name, &anError);  

    ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(phone, [modelObj phone], kABPersonPhoneMainLabel, NULL);

    ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &anError);
    CFRelease(phone);

    NSString *address = [NSString stringWithFormat:@"%@ %@", [modelObj addr1], [modelObj addr2]];
    NSMutableDictionary *dictionaryAddress = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dictionaryAddress setObject:address forKey:(NSString *)kABPersonAddressStreetKey];
    [dictionaryAddress setObject:@"us" forKey:(NSString *)kABPersonAddressCountryCodeKey];

    ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABDictionaryPropertyType);
    ABMultiValueAddValueAndLabel(address, dictionaryAddress, kABPersonAddressStreetKey, NULL);
    [dictionaryAddress release];

    ABRecordSetValue(aContact, kABPersonAddressProperty, address, &anError);
    CFRelease(address); 

    if (anError) {
        aContact = nil;
    }

    [(id)aContact autorelease];

    return (id)aContact;
}

Found an answer: It's nicely documented in iOS Developer Library:
http://developer.apple.com/library/ios/#samplecode/QuickContacts/Listings/Classes_QuickContactsViewController_m.html#//apple_ref/doc/uid/DTS40009475-Classes_QuickContactsViewController_m-DontLinkElementID_6

Here is a sample code I implemented to return a ABPersonRecordRef as an object. The error I had experienced was related to not retaining the ABPersonRecordRef object after returning it.

- (id)personRecordUsingModelObj:(id)modelObj {
    ABRecordRef aContact = ABPersonCreate();
    CFErrorRef anError = NULL;

    NSString *name = [NSString stringWithFormat:@"%@", [modelObj name]];
    ABRecordSetValue(aContact, kABPersonOrganizationProperty, name, &anError);  

    ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(phone, [modelObj phone], kABPersonPhoneMainLabel, NULL);

    ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &anError);
    CFRelease(phone);

    NSString *address = [NSString stringWithFormat:@"%@ %@", [modelObj addr1], [modelObj addr2]];
    NSMutableDictionary *dictionaryAddress = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dictionaryAddress setObject:address forKey:(NSString *)kABPersonAddressStreetKey];
    [dictionaryAddress setObject:@"us" forKey:(NSString *)kABPersonAddressCountryCodeKey];

    ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABDictionaryPropertyType);
    ABMultiValueAddValueAndLabel(address, dictionaryAddress, kABPersonAddressStreetKey, NULL);
    [dictionaryAddress release];

    ABRecordSetValue(aContact, kABPersonAddressProperty, address, &anError);
    CFRelease(address); 

    if (anError) {
        aContact = nil;
    }

    [(id)aContact autorelease];

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