博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

 

 

posted on 2011-05-04 18:37  Likwo  阅读(1696)  评论(0编辑  收藏  举报