0

I need help with a custom AddressBook (ABPeoplePickerViewController) for iPhone? I want to have an array with all my contacts, pulling just their name and numbers into the cells of the tableview to display.. Select a few contacts, open Messages and send them a Text/SMS with a custom message..

WhatsApp messenger is an awesome example, if you go to Settings, Tell a Friend, then Message.. I want that look!

Contacts Pickerview It must be custom as I also want the Send and Cancel buttons below, and Name in cell.textLabel and their number in the cell.detailTextLabel in a Subtitle style tableview.

So how do I get their details from addressbook and into my arrays (contactsName, contactsNumber)? Thanks in advance! Here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [contactsName objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [contactsNumber objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [self dismissViewControllerAnimated:YES completion:^{ NSLog(@"Message controller has been canceled"); }];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    NSString *name = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *number = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSLog(@"Name: %@ Number: %@", name, number);

    return NO;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    return NO;
}

- (void)openSMScontroller {
    MFMessageComposeViewController *smsView = [[MFMessageComposeViewController alloc] init];
    smsView.messageComposeDelegate = self;

    smsView.recipients = [NSArray arrayWithArray:contactsNumber];
    smsView.body = @"Check out this awesome app!";

    [self presentModalViewController:smsView animated:YES];
}
emotality
  • 12,795
  • 4
  • 39
  • 60

2 Answers2

2

you can get what you want from addressbook by below code my friend.!!!!!

happy coding!!!!!!

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
 ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
   ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);
 NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty);
if([address count] > 0)
{
    for (NSDictionary *addressDict in address)
    {
        countrytf.text = [addressDict objectForKey:@"Country"];
        streetaddresstf.text = [addressDict objectForKey:@"Street"];
        citynametf.text = [addressDict objectForKey:@"City"];
        statenametf.text = [addressDict objectForKey:@"State"];
        zipcodetf.text = [addressDict objectForKey:@"ZIP"];

    }
    CFRelease(addressProperty);
}
}
NiravPatel
  • 3,260
  • 2
  • 21
  • 31
  • Thanks for your answer, will give it a try later, if it works ill mark your answer, thanks.. :) @NiravPatel – emotality Dec 11 '12 at 10:14
  • What is "**countrytf**" etc? I dont want address, just numbers and name, but if i remove those address lines i get warnings for **phoneNumbers** and **addressProperty**? Can you maybe explain or just give me another code for it please? :< thanks for the help! @NiravPatel – emotality Dec 11 '12 at 12:32
  • countrytf,streetaddresstf etc are just textfield that i have used in my app, you can put NSString instead of this my friend... for getting phone numbers you can do following ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); NSString* phone = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0); nslog(@"phone:%@",phone); – NiravPatel Dec 11 '12 at 12:43
  • Can you maybe tell me how to put the selected contacts (name, number) into 1 dictionary then send the text/sms to the selected contacts? Just a direction please? – emotality Jan 04 '13 at 11:50
0

To get phone number...

ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); 
 NSString*    phone = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0); 

 nslog(@"phone:%@",phone); 

let me know it is working or not !!!!!!please if it is right then reward it and i know it is right..

Happy Coding

NiravPatel
  • 3,260
  • 2
  • 21
  • 31