如何使用ABUnknow Person ViewController初始输入生成的数据?

时间:2011-04-15 08:43:45

标签: ios addressbook addressbookui

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

情况:

1)对象将返回名称为address1,address2,phone:

NSString个对象
[anObject name];
[anObject address1];
[anObject address2];
[anObject name];

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

我查看过iOS文档并通过Google和StackOverflow搜索,找不到适合这种简单情况的正确答案。

有人可以指导我吗?

1 个答案:

答案 0 :(得分:2)

找到答案: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

这是我为将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;
}