views:

571

answers:

1

hi guys. I'm really struggling with ABAddressBookGetPersonWithRecordID at the moment. I am saving an ID, and then trying to call it back up again. Currently im doing something simple to test the linking, but its not working.

First, I can read objects from my iphone simulator address book using:

-(BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    NSString *contactName;
    NSString *contactCompany;
    NSString *contactFirst;
    NSString *contactLast;


    contactFirst = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) stringByAppendingString:@" "];    
    contactLast =  (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    contactName = [contactFirst stringByAppendingString:contactLast];

    contactCompany = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);


    NSNumber *recordId = [NSNumber numberWithInteger: ABRecordGetRecordID(person)];

    NSLog(@"record id is %d", recordId);
    NSLog(@"Person Reference: %d", person);
    NSLog(@"Name: %@", contactName);
    NSLog(@"Company: %@", contactCompany);

Which has the NSLog:

2010-01-26 11:52:31.396 SQL[19786:207] record id is 69283952
2010-01-26 11:52:31.397 SQL[19786:207] Person Reference: 69495792
2010-01-26 11:52:31.398 SQL[19786:207] Name: John Adams
2010-01-26 11:52:31.398 SQL[19786:207] Company: (null)

So my presumption is that it all works on that end. The problem is using the 'record id' 69283952, to call this contact info back up. I am currently trying to do it like this:

-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)newindexPath {


    ABAddressBookRef ab = ABAddressBookCreate();
    ABPersonViewController *pvc = [[ABPersonViewController alloc] init];

    ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,69283952);
    NSLog(@"person - %d", person);
    pvc.displayedPerson = person;
    NSLog(@"pvc.displayedPerson - %d", pvc.displayedPerson);
    pvc.addressBook = ab;
    pvc.allowsEditing = YES;
    pvc.personViewDelegate = self;
    [[self navigationController] pushViewController:pvc animated:YES];
    [pvc release];

Which has the NSLog

2010-01-26 11:58:19.393 SQL[19849:207] Looking Up Contact Now
2010-01-26 11:58:19.399 SQL[19849:207] person - 0
2010-01-26 11:58:19.400 SQL[19849:207] pvc.displayedPerson - 0

And thus all i get is a null person. What am I doing wrong? I have absolutely no idea!

regards, @norskben

+1  A: 

NSNumber is an object, not an integer.

To insert an NSNumber object into a format string (such as for NSLog), you should use %@ (not %d).

NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(person)];
NSLog(@"record id is %@",recordId);

Similarly, if you have the recordID in an NSNumber object, you can get the integer value using integerValue:

ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,recordId.integerValue);
gerry3
interesting, so the recordId is a relatively small number (i get 2), and nothing like the old 67238032. ANd there you go, what a tiny fix!!! thank you so much gerry3
norskben
Yep, 69283952 was the pointer value (memory address) of your NSNumber object.
gerry3
hmmm..these pointers keep tricking me.
norskben
I know the feeling about "pointers". I have the same issue...but it goes away with using them for awhile. :)
Jann