这段代码示范如何读取iphone的Address book
就透过AddressBook相关的SDK取出就行了
- ABAddressBookRef addressBook = ABAddressBookCreate();
- CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
- CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
- NSMutableArray *masterList = [[NSMutableArray alloc] init];
- for (int i = 0; i < nPeople; i++) {
- ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
- CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
- CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
- NSString *contactFirstLast = [NSString stringWithFormat: @"%@", (NSString *)lastName];
- CFRelease(firstName);
- CFRelease(lastName);
- [masterList addObject:contactFirstLast];
- [contactFirstLast release];
- }
- self.list = masterList;
- [masterList release];
这段代码示范如何新增一笔资料到通讯录中
- -(void)editar:(id)sender{
- ABAddressBookRef libroDirec = ABAddressBookCreate();
- ABRecordRef persona = ABPersonCreate();
- ABRecordSetValue(persona, kABPersonFirstNameProperty, @"kate" , nil);
- ABRecordSetValue(persona, kABPersonLastNameProperty, @"Hutson", nil);
- ABAddressBookAddRecord(libroDirec, persona, nil);
- ABAddressBookSave(libroDirec, nil);
- CFRelease(persona);
- }
这段代码示范如何删除通讯录资料
- -(void)editar:(id)sender
- {
- ABAddressBookRef libroDirec = ABAddressBookCreate();
- CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(libroDirec);
- CFIndex xPeople = ABAddressBookGetPersonCount(libroDirec);
- for (int i=0; i <xPeople; i++ )
- {
- ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
- CFStringRef nombreX = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
- CFStringRef cadena = CFSTR("kate");
- if (CFStringCompare(nombreX, cadena , 0) == kCFCompareEqualTo )
- {
- ABAddressBookRemoveRecord(libroDirec, ref, nil);
- ABAddressBookSave(libroDirec, nil);
- }
- }
- }