通讯录总结

  最近本人在做通讯录管理功能,做了三个星期,现在自己对通讯录这块操作总结一下。还有本人不善于写博客,所以很多都会简洁明了,用的到的拿去,有什么建议的随时交流。

一、通讯录的操作

1.如何获取所有的联系人:

记得创建单例:

+ (AddressBook*)sharedInstance {

      static dispatch_once_t pred;

      static AddressBook *instance = nil;

      dispatch_once(&pred, ^{

          instance = [[AddressBook alloc] init];  

      });

      return instance;

}

-(NSArray*)readAllPeoples{

    NSMutableArray *totalArr = [[[NSMutableArray alloc] init] autorelease];

    CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(_addressBook);

    if(results){

        for(int i = 0; i < CFArrayGetCount(results); i++){

            ABRecordRef person = CFArrayGetValueAtIndex(results, i);

            if ([self countOfPhones:person] < 50) {

                NSDictionary *personInfoDic = [self getPersonInfo:person];

                [totalArr addObject:personInfoDic];

            }

        }

        CFRelease(results);

    }

    return [NSArray arrayWithArray:totalArr];

}

2.如何获取单个联系人的所有信息:(通过调用如下方法)

(1)获取电话

- (NSArray*)getPersonPhones:(NSString*)recordID{

    ABRecordRef person = ABAddressBookGetPersonWithRecordID(_addressBook, recordID.intValue);

    return [self getAboutData:person ID:kABPersonPhoneProperty];

}

(2)获取邮箱 

- (NSArray*)getPersonEmails:(NSString*)recordID{

    ABRecordRef person = ABAddressBookGetPersonWithRecordID(_addressBook, recordID.intValue);

    return [self getAboutData:person ID:kABPersonEmailProperty];

}

 (3)获取姓名

- (NSString*)getPersonCompositeName:(NSString*)recordID{

    ABRecordRef person = ABAddressBookGetPersonWithRecordID(_addressBook, recordID.intValue);

    return [self getAboutCompositeName:person];

}

 (4)获取首页

- (NSArray *)getAboutURL:(ABRecordRef)person{

    return [self getAboutData:person ID:kABPersonURLProperty];

}

以上四个方法需要调用如下方法:

- (NSArray*)getAboutData:(ABRecordRef)person ID:(ABPropertyID)ABPropertyID{

    NSMutableArray *values = [[[NSMutableArray alloc] init] autorelease];

    ABMultiValueRef property = ABRecordCopyValue(person, ABPropertyID);

    if(property){

        for (int k = 0; k < ABMultiValueGetCount(property); k++){

            NSString *label = @"";

            NSString *value = @"";

            CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(property, k);

            CFStringRef valueRef = ABMultiValueCopyValueAtIndex(property, k);

            if(valueRef){

                value = [NSString stringWithFormat:@"%@",valueRef];

                CFRelease(valueRef);

            }            

            if(labelRef){

                CFStringRef localizedLabelRef = ABAddressBookCopyLocalizedLabel(labelRef);

                if(localizedLabelRef){

                    label = [NSString stringWithFormat:@"%@",localizedLabelRef];

                    CFRelease(localizedLabelRef);

                }

                else

                    label = [NSString stringWithFormat:@"%@",labelRef];

                

                CFRelease(labelRef);

            }

            [values addObject:@[value,label]];

        }

        CFRelease(property);

    }

    return [NSArray arrayWithArray:values];

}

 (5)获取地址

- (NSArray*)getAboutAddress:(ABRecordRef)person{

    NSMutableArray *addressValues = [[[NSMutableArray alloc] init] autorelease];

    ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);

    if(address){

        for(int k = 0; k < ABMultiValueGetCount(address); k++){

            NSMutableDictionary *addressSubValues = [[[NSMutableDictionary alloc] init] autorelease];

            NSString *label = @"";

            CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(address, k);

            if(labelRef){

                CFStringRef localizedLabelRef = ABAddressBookCopyLocalizedLabel(labelRef);

                if(localizedLabelRef){

                    label = [NSString stringWithFormat:@"%@",localizedLabelRef];

                    CFRelease(localizedLabelRef);

                }

                else

                    label = [NSString stringWithFormat:@"%@",labelRef];

                

                CFRelease(labelRef);

            }

            

            CFDictionaryRef addressDiRef = ABMultiValueCopyValueAtIndex(address, k);

            if(addressDiRef){

                NSDictionary *personaddress =(NSDictionary*)addressDiRef;

                NSString *country = [personaddress valueForKey:(NSString*)kABPersonAddressCountryKey];

                NSString *city = [personaddress valueForKey:(NSString*)kABPersonAddressCityKey];

                NSString *street = [personaddress valueForKey:(NSString*)kABPersonAddressStreetKey];

                NSString *zip = [personaddress valueForKey:(NSString*)kABPersonAddressZIPKey];

                NSString *coutntryCode = [personaddress valueForKey:(NSString*)kABPersonAddressCountryCodeKey];

                NSString *state = [personaddress valueForKey:(NSString*)kABPersonAddressStateKey];

                country = country.length>0 ? country : @"";

                city = city.length>0 ? city : @"";

                street = street.length>0 ? street : @"";

                zip = zip.length>0 ? zip : @"";

                coutntryCode = coutntryCode.length>0 ? coutntryCode : @"";

                state = state.length>0 ? state : @"";

                

                [addressSubValues setObject:country forKey:CountryKey];

                [addressSubValues setObject:city forKey:CityKey];

                [addressSubValues setObject:street forKey:StreetKey];

                [addressSubValues setObject:zip forKey:ZipKey];

                [addressSubValues setObject:coutntryCode forKey:CoutntrycodeKey];

                [addressSubValues setObject:state forKey:StateKey];

                CFRelease(addressDiRef);

            }

            [addressValues addObject:@[addressSubValues,label]];

        }

        CFRelease(address);

    }

    return [NSArray arrayWithArray:addressValues];

}

 (6)获取生日

- (NSDate*)getAboutBirthday:(ABRecordRef)person{

    NSDate *birthdayValues = nil;

    CFDateRef birthdayRef = ABRecordCopyValue(person, kABPersonBirthdayProperty);

    if(birthdayRef){

        birthdayValues = (NSDate*)birthdayRef;

        CFRelease(birthdayRef);

    }

    return  birthdayValues;

}

 (7)获取日期

- (NSArray*)getAboutDate:(ABRecordRef)person{

    return [self getAboutData:person ID:kABPersonDateProperty];

}

 (8)获取关系 

- (NSArray*)getAboutRelate:(ABRecordRef)person{

    return [self getAboutData:person ID:kABPersonRelatedNamesProperty];

}

 (9)获取社交

- (NSArray*)getAboutSocialProfile:(ABRecordRef)person{

    NSMutableArray *values = [[[NSMutableArray alloc] init] autorelease];

    ABMultiValueRef property = ABRecordCopyValue(person, kABPersonSocialProfileProperty);

    if(property){

        for (int k = 0; k < ABMultiValueGetCount(property); k++){

            NSString *label = @"";

            NSMutableDictionary *value = [NSMutableDictionary dictionary];

            CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(property, k);

            CFDictionaryRef valueRef = ABMultiValueCopyValueAtIndex(property, k);

            if(labelRef){

                CFStringRef localizedLabelRef = ABAddressBookCopyLocalizedLabel(labelRef);

                if(localizedLabelRef){

                    label = [NSString stringWithFormat:@"%@",localizedLabelRef];

                    CFRelease(localizedLabelRef);

                }

                else

                    label = [NSString stringWithFormat:@"%@",labelRef];

                

                CFRelease(labelRef);

            }

            

            if(valueRef){

                [value setDictionary:(NSDictionary*)valueRef];

                CFRelease(valueRef);

            }

            [values addObject:@[value,label]];

        }

        CFRelease(property);

    }

    

    return [NSArray arrayWithArray:values];

}

 (10)获取IM

- (NSArray*)getAboutIM:(ABRecordRef)person{

    NSMutableArray *values = [[[NSMutableArray alloc] init] autorelease];

    ABMultiValueRef property = ABRecordCopyValue(person, kABPersonInstantMessageProperty);

    if(property){

        for (int k = 0; k < ABMultiValueGetCount(property); k++){

            NSString *label = @"";

            NSMutableDictionary *value = [NSMutableDictionary dictionary];

            CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(property, k);

            CFDictionaryRef valueRef = ABMultiValueCopyValueAtIndex(property, k);

            if(labelRef){

                CFStringRef localizedLabelRef = ABAddressBookCopyLocalizedLabel(labelRef);

                if(localizedLabelRef){

                    label = [NSString stringWithFormat:@"%@",localizedLabelRef];

                    CFRelease(localizedLabelRef);

                }

                else

                    label = [NSString stringWithFormat:@"%@",labelRef];

                

                CFRelease(labelRef);

            }

            

            if(valueRef){

                [value setDictionary:(NSDictionary*)valueRef];

                CFRelease(valueRef);

            }

            [values addObject:@[value,label]];

        }

        CFRelease(property);

    }

    

    return [NSArray arrayWithArray:values];

}

 (11)获取备注

- (NSString*)getAboutNote:(ABRecordRef)person{

    NSString *note = @"";

    CFStringRef noteRef = ABRecordCopyValue(person,kABPersonNoteProperty);

    if(noteRef){

        note = [NSString stringWithFormat:@"%@",noteRef];

        CFRelease(noteRef);

    }

    return note;

}

 (12)获取头像

- (UIImage*)getHeaderImage:(NSString*)recordID{

    UIImage *image = nil;

    ABRecordRef person = ABAddressBookGetPersonWithRecordID(_addressBook, recordID.intValue);

    if(person){

        CFDataRef dateRef = ABPersonCopyImageData(person);

        if(dateRef){

            image = [UIImage imageWithData:(NSData*)dateRef];

            CFRelease(dateRef);

        }

    }

    return image;

}

3.如何添加联系人

(1)创建个模型类(记住里面需要一个联系人的所有属性,具体看你自己的数据结构是如何的)

 BDContact *contact          = [BDContact new];

(2) 设置所有属性,自己判断

if (allNumberArray.count > 0)

    {

        [contact setPhones:allNumberArray];

    }

    if (addressesArray.count > 0)

    {

        [contact setAddress:addressesArray];

    }

    if (allEmailArray.count > 0)

    {

        [contact setEmails:allEmailArray];

    }

    if (allUrlArray.count > 0)

    {

        [contact setURLs:allUrlArray];

    }

    if (allDateArray.count > 0)

    {

        [contact setDates:allDateArray];

    }

    if (allRelateArray.count > 0)

    {

        [contact setRelates:allRelateArray];

    }

    if (allSocialArray.count > 0)

    {

        [contact setSocials:allSocialArray];

    }

    if (allIMsArray.count > 0)

    {

        [contact setIMs:allIMsArray];

    }

(3)通过ADdressBook调用以下方法将数据传进去

- (BOOL)createNewPersonWithContact:(BDContact*)contact{

    ABRecordRef record = ABPersonCreate();

    [self setNameWithPerson:record nameArr:[contact.personInfo objectForKey:PersonNameKey] type:REPLACE];

    [self setPhoneWithPerson:record phoneArr:[contact.personInfo objectForKey:PersonPhoneKey] type:REPLACE];

    [self setEmailWithPerson:record emailArr:[contact.personInfo objectForKey:PersonEmailKey] type:REPLACE];

    [self setURLWithPerson:record urlArr:[contact.personInfo objectForKey:PersonUrlKey] type:REPLACE];

    [self setAddressWithPerson:record addressArr:[contact.personInfo objectForKey:PersonAddressKey] type:REPLACE];

    [self setBirthdayWithPerson:record birthday:[NSDate dateFromString:[contact.personInfo objectForKey:PersonBirthdayKey]] type:REPLACE];

    [self setNoteWithPerson:record note:[contact.personInfo objectForKey:PersonNoteKey]];

    [self setDateWithPerson:record dateArr:[contact.personInfo objectForKey:PersonDateKey] type:REPLACE];

    [self setRelateWithPerson:record relateArr:[contact.personInfo objectForKey:PersonRelateKey] type:REPLACE];

    [self setSocialWithPerson:record socialArr:[contact.personInfo objectForKey:PersonSocialProfileKey] type:REPLACE];

    [self setIMWithPerson:record IMArr:[contact.personInfo objectForKey:PersonIMKey] type:REPLACE];

    [self setHeaderImageWithPerson:record image:[contact.personInfo objectForKey:PersonHeaderImageKey]];

 

    ABAddressBookAddRecord(_addressBook, record, nil);

    BOOL result = ABAddressBookSave(_addressBook, nil);

    CFRelease(record);

    

    return result;

}

看得懂如下其中一个set方法,应该其他就自己可以写了:

- (void)setEmailWithPerson:(ABRecordRef)person emailArr:(NSArray *)email_bp type:(NSInteger)type{

    NSArray *email_ad = [self getAboutEmail:person];

    NSArray *emails = type==MERGE ? [NSArray mergeWithArray:email_ad and:email_bp] : email_bp;

    ABMutableMultiValueRef multiEmails = ABMultiValueCreateMutable(kABMultiStringPropertyType);

    for (int i = 0; i < emails.count; i++) {

        NSArray *emailArr = [emails objectAtIndex:i];

        NSString *email = [emailArr objectAtIndex:0];

        NSString *label = [emailArr objectAtIndex:1];

        ABMultiValueAddValueAndLabel(multiEmails, email, (CFStringRef)label, NULL);

    }

    ABRecordSetValue(person, kABPersonEmailProperty, multiEmails, nil);

    CFRelease(multiEmails);

}


4.如何通过联系人ID跳转至编辑联系人编辑界面

NSString *recordID = [contact getRecordID];

    ABRecordRef person = [[AddressBook sharedInstance] getPersonWithRecordID:recordID];

    ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];

    picker.navigationItem.title = @"编辑联系人";

    picker.displayedPerson = person;

    picker.newPersonViewDelegate = self;

  

    UIViewController *vc = [[UIViewController alloc] init];

    vc.view.backgroundColor = [UIColor whiteColor];

    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:vc];

    [self.navigationController presentViewController:navigation animated:YES completion:^{

        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;

        [navigation pushViewController:picker animated:NO];

    }];

记得实现代理方法:

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person{

    [newPersonView.navigationController popViewControllerAnimated:NO];

    [self.navigationController dismissViewControllerAnimated:YES completion:^{

        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

    }];

}

~~~~~~!!!就暂时写到这里,喜欢的就拿去吧,作者很大方的!!!~~~~~~~~

 

 

 

 

原文地址:https://www.cnblogs.com/destiLaugh/p/6141340.html