0

Tried using the following code in my viewdidload and I cannot seem to get a test contact loaded onto my test iphone (running IOS 6). Any suggestions as to why this does not work?

CFErrorRef* error;
// create address book record
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(nil,error);
// create a person
ABRecordRef person = ABPersonCreate();
// first name of the new person
ABRecordSetValue(person, kABPersonFirstNameProperty, @"FirstName" , nil);
// his last name
ABRecordSetValue(person, kABPersonLastNameProperty, @"LastName", nil);
//add the new person to the record
ABAddressBookAddRecord(addressBook, person, nil);

ABRecordRef group = ABGroupCreate(); //create a group
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", error); // set group's name
ABGroupAddMember(group, person, error); // add the person to the group
ABAddressBookAddRecord(addressBook, group, error); // add the group

//save the record
ABAddressBookSave(addressBook, nil);

// relase the ABRecordRef  variable
CFRelease(person);
  • This is probably a permission problem. See http://stackoverflow.com/questions/12517394/ios-6-address-book-not-working?rq=1 – rmaddy Nov 20 '12 at 23:54
  • See http://stackoverflow.com/questions/14018131/iphone-address-book-phone-numbers/14018453#14018453 – Prasad G Apr 18 '13 at 06:19

2 Answers2

0

Have a look at my question:

It was meant for OSX, but i think it applies to iOS as well:

How to update: (COCOA/OSX) ABPerson / ABMultiValue (phonenumbers)?

Community
  • 1
  • 1
Roger
  • 7,535
  • 5
  • 41
  • 63
0
 NSMutableArray*   contactList=[[NSMutableArray alloc]initWithCapacity:0];
        ABAddressBookRef m_addressbook = ABAddressBookCreate();
        if (!m_addressbook) {
            NSLog(@"opening address book");
        }
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
        CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
        for ( int i=0;i < nPeople;i++) {
            NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
            ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
            CFStringRef firstName;
            NSString* lastName;
            firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
            lastName  = (NSString*)ABRecordCopyValue(ref, kABPersonLastNameProperty);
            if(!lastName){
                lastName=@"";
            }
            [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName,lastName] forKey:@"name"];
            //For Email ids
            ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);
            if(ABMultiValueGetCount(eMail) > 0) {
                [dOfPerson setObject:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];
            }
            //Birthday
            CFStringRef birthday  = ABRecordCopyValue(ref, kABPersonBirthdayProperty);
            if(birthday){
                [dOfPerson setObject: (NSDate*)birthday forKey:@"birthday"];
            }
            ABRecordRef record = CFArrayGetValueAtIndex(allPeople,i);
            //******* RecordID
            NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(record)];
            NSString *recordStr=[NSString stringWithFormat:@"%@",recordId];
            [dOfPerson setObject:recordStr forKey:@"recordId"];
            //********
            ABMultiValueRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty);
            //For Phone number
            NSString* mobileLabel;
            for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
            {
                mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
                if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
                {
                    [dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                }
                else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
                {
                    [dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                    break ;
                }
            }
            ABMultiValueRef anniversaries = ABRecordCopyValue(record, kABPersonDateProperty);
            NSString *anniversaryLabel;
            for (CFIndex j=0; j < ABMultiValueGetCount(anniversaries); j++) {
                anniversaryLabel = (NSString*)ABMultiValueCopyLabelAtIndex(anniversaries, j);
                if([anniversaryLabel isEqualToString:(NSString *)kABPersonAnniversaryLabel])
                {
                    NSDate *anniversaryDate=(NSDate *)ABMultiValueCopyValueAtIndex(anniversaries, j);
                    NSLog(@"%@",anniversaryDate);
                    [dOfPerson setObject:anniversaryDate forKey:@"anniversaryDate"];
                }
            }
            [contactList addObject:dOfPerson];
        }