0

In iOS I'm trying to create a new entry in the address book via the ABUnknownPersonViewController with this code (it's an UITableViewController):

- (void)                       tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *phoneNumber = [[cell textLabel] text];
    ABRecordRef record = ABPersonCreate();
    CFErrorRef error = NULL;
    ABMutableMultiValueRef phone =
            ABMultiValueCreateMutable(kABPersonPhoneProperty);
    ABMultiValueAddValueAndLabel
            (phone, phoneNumber, kABPersonPhoneMobileLabel, NULL);
    bool didSet = ABRecordSetValue(record, kABPersonPhoneProperty, phone,
            &error);

    if (!didSet) {
        return;
    }
    ABUnknownPersonViewController *viewController =
            [[[ABUnknownPersonViewController alloc] init] autorelease];

    [viewController setUnknownPersonViewDelegate:self];
    [viewController setAllowsAddingToAddressBook:YES];
    [viewController setAllowsActions:NO];
    [viewController setDisplayedPerson:record];
    [[self navigationController] setNavigationBarHidden:NO animated:NO];
    [[self navigationController] pushViewController:viewController
            animated:YES];
    CFRelease(record);
    CFRelease(phone);
}

Edit: but it crashes apparently because the phoneNumber is formatted before is added to the address book (e.j. "987654321" becomes "987 654 321"). If I replace the phoneNumber with a constant string (@"987654321") everything runs alright. Also if I add value not for the ABPersonPhoneProperty but for ABPersonEmailProperty runs properly.

Any help?

caliopigio
  • 48
  • 3
  • Have you tried creating a @property (copy) NSString *selectedPhoneNumber? If you create a property, when you assign it the textLabel text, it will copy. When next cell is selected, it will release and copy. – bryanmac Dec 29 '11 at 21:06
  • What do you mean by "any other string"? A constant @"string"? Any string created using [[NSString alloc] init...]? –  Dec 29 '11 at 21:11
  • @bryanmac assigning the copied property doesn't resolved the problem. I'll keep trying. – caliopigio Dec 29 '11 at 22:32
  • @H2CO3 with a constant string it works but creating it with alloc-init doesn't, not even with the convenience methods stringWith... – caliopigio Dec 29 '11 at 22:35
  • Try sending it a [[ retain] autorelease] message... –  Dec 30 '11 at 00:27
  • After you assigned the copied property, did you pass the property to AB methods? – bryanmac Dec 30 '11 at 01:16
  • @bryanmac yes, I replace every phoneNumber with selectedPhoneNumber – caliopigio Dec 30 '11 at 02:28
  • it crashes apparently because the phoneNumber is formatted before is added to the address book (e.j. "987654321" becomes "987 654 321"). If I replace the phoneNumber with a constant string (@"987654321") everything runs alright. Also if I add value not for the ABPersonPhoneProperty but for ABPersonEmailProperty runs properly. – caliopigio Jan 03 '12 at 17:12

1 Answers1

0

It's working while I replace:

ABMutableMultiValueRef phone = ABMultiValueCreateMutable(kABPersonPhoneProperty);

with:

ABMutableMultiValueRef phone = ABMultiValueCreateMutable(kABPersonEmailProperty);

It's not ideal but works.

caliopigio
  • 48
  • 3