Iphone里如何针对地址本编程,今天特地学习了下,作了个简单的例子,作个笔记
1.首先添加framwork,添加AddressBook.framework
2.具体直接上代码
#import <AddressBook/AddressBook.h>
@implementation AddressHelp
- (void)addAddress
{
ABAddressBookRef ab = ABAddressBookCreate();
CFErrorRef error;
CFStringRef nickname = CFSTR("Sparky");
CFStringRef name = CFSTR("TestName");
ABRecordRef record = ABPersonCreate();
BOOL isSuccess ;
isSuccess = ABRecordSetValue(record, kABPersonNicknameProperty,nickname, &error);
isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, name, &error);
isSuccess = ABAddressBookSave(ab, &error);
ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);
CFTypeRef phone= CFSTR("123000222111");
ABMultiValueAddValueAndLabel(copyOfPhones, phone,kABPersonPhoneMobileLabel,NULL);
isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);
isSuccess = ABAddressBookAddRecord(ab, record, &error);
isSuccess = ABAddressBookSave(ab, &error);
NSLog(@"is success %d", isSuccess);
}
//NSArray : ABRecordRef
- (NSArray *)getAllAddresses
{
ABAddressBookRef ab = ABAddressBookCreate();
NSArray *array = (NSArray *)ABAddressBookCopyArrayOfAllPeople(ab);
NSLog(@"Retrieved %d contacts\n", [ array count ]);
for (int i = 0; i < [array count] ; i++)
{
ABRecordRef record = [array objectAtIndex:i];
NSLog(@" name is %@ ", ABRecordCopyValue(record, kABPersonMiddleNameProperty) );
ABMultiValueRef phoneNumbers = ABRecordCopyValue(record, kABPersonPhoneProperty);
CFIndex nPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (int i = 0; i < nPhoneNumbers ; i++)
{
CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
if ([(NSString *)label isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
CFStringRef mobilePhone = ABMultiValueCopyValueAtIndex(phoneNumbers, i);
NSString *mobilePhoneStr = (NSString *)mobilePhone;
NSLog(@"phone mobile is : %@", mobilePhoneStr);
}
}
}
return array;
}
- (void)addGroup
{
//Create group
ABAddressBookRef ab = ABAddressBookCreate();
CFErrorRef error;
ABRecordRef group = ABGroupCreate();
ABRecordSetValue(group, kABGroupNameProperty,@"new group", &error);
ABAddressBookAddRecord(ab, group, &error);
ABAddressBookSave(ab, &error);
//Create new person and save to this group
ABRecordRef record = ABPersonCreate();
BOOL isSuccess ;
isSuccess = ABRecordSetValue(record, kABPersonNicknameProperty,@"GroupMember nick name", &error);
isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, @"Middle name", &error);
ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);
CFTypeRef phone= CFSTR("123000222111");
ABMultiValueAddValueAndLabel(copyOfPhones, phone,kABPersonPhoneMobileLabel,NULL);
isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);
isSuccess = ABAddressBookAddRecord(ab, record, &error);
isSuccess = ABAddressBookSave(ab, &error);
ABGroupAddMember(group, record, &error);
NSLog(@"is success %d", isSuccess);
ABAddressBookSave(ab, &error);
CFRelease(group);
}
- (NSArray *)getGroups
{
NSMutableArray *groupNames = [[[NSMutableArray alloc] init] autorelease];
ABAddressBookRef ab = ABAddressBookCreate();
NSArray *array = (NSArray *)ABAddressBookCopyArrayOfAllGroups(ab);
for (int i = 0 ; i < [array count]; i++)
{
ABRecordRef group = [array objectAtIndex:i];
CFTypeRef groupName = ABRecordCopyValue(group, kABGroupNameProperty);
NSString *groupNameStr = [NSString stringWithFormat:@"%@", (NSString *)groupName];
NSLog(@"groupNames is %@", groupNameStr);
[groupNames addObject:groupNameStr];
CFRelease(groupName);
}
return groupNames;
}
@end
@implementation AddressHelp
- (void)addAddress
{
ABAddressBookRef ab = ABAddressBookCreate();
CFErrorRef error;
CFStringRef nickname = CFSTR("Sparky");
CFStringRef name = CFSTR("TestName");
ABRecordRef record = ABPersonCreate();
BOOL isSuccess ;
isSuccess = ABRecordSetValue(record, kABPersonNicknameProperty,nickname, &error);
isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, name, &error);
isSuccess = ABAddressBookSave(ab, &error);
ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);
CFTypeRef phone= CFSTR("123000222111");
ABMultiValueAddValueAndLabel(copyOfPhones, phone,kABPersonPhoneMobileLabel,NULL);
isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);
isSuccess = ABAddressBookAddRecord(ab, record, &error);
isSuccess = ABAddressBookSave(ab, &error);
NSLog(@"is success %d", isSuccess);
}
//NSArray : ABRecordRef
- (NSArray *)getAllAddresses
{
ABAddressBookRef ab = ABAddressBookCreate();
NSArray *array = (NSArray *)ABAddressBookCopyArrayOfAllPeople(ab);
NSLog(@"Retrieved %d contacts\n", [ array count ]);
for (int i = 0; i < [array count] ; i++)
{
ABRecordRef record = [array objectAtIndex:i];
NSLog(@" name is %@ ", ABRecordCopyValue(record, kABPersonMiddleNameProperty) );
ABMultiValueRef phoneNumbers = ABRecordCopyValue(record, kABPersonPhoneProperty);
CFIndex nPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (int i = 0; i < nPhoneNumbers ; i++)
{
CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
if ([(NSString *)label isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
CFStringRef mobilePhone = ABMultiValueCopyValueAtIndex(phoneNumbers, i);
NSString *mobilePhoneStr = (NSString *)mobilePhone;
NSLog(@"phone mobile is : %@", mobilePhoneStr);
}
}
}
return array;
}
- (void)addGroup
{
//Create group
ABAddressBookRef ab = ABAddressBookCreate();
CFErrorRef error;
ABRecordRef group = ABGroupCreate();
ABRecordSetValue(group, kABGroupNameProperty,@"new group", &error);
ABAddressBookAddRecord(ab, group, &error);
ABAddressBookSave(ab, &error);
//Create new person and save to this group
ABRecordRef record = ABPersonCreate();
BOOL isSuccess ;
isSuccess = ABRecordSetValue(record, kABPersonNicknameProperty,@"GroupMember nick name", &error);
isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, @"Middle name", &error);
ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);
CFTypeRef phone= CFSTR("123000222111");
ABMultiValueAddValueAndLabel(copyOfPhones, phone,kABPersonPhoneMobileLabel,NULL);
isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);
isSuccess = ABAddressBookAddRecord(ab, record, &error);
isSuccess = ABAddressBookSave(ab, &error);
ABGroupAddMember(group, record, &error);
NSLog(@"is success %d", isSuccess);
ABAddressBookSave(ab, &error);
CFRelease(group);
}
- (NSArray *)getGroups
{
NSMutableArray *groupNames = [[[NSMutableArray alloc] init] autorelease];
ABAddressBookRef ab = ABAddressBookCreate();
NSArray *array = (NSArray *)ABAddressBookCopyArrayOfAllGroups(ab);
for (int i = 0 ; i < [array count]; i++)
{
ABRecordRef group = [array objectAtIndex:i];
CFTypeRef groupName = ABRecordCopyValue(group, kABGroupNameProperty);
NSString *groupNameStr = [NSString stringWithFormat:@"%@", (NSString *)groupName];
NSLog(@"groupNames is %@", groupNameStr);
[groupNames addObject:groupNameStr];
CFRelease(groupName);
}
return groupNames;
}
@end