ios操作通讯录的例子

来源:互联网 发布:模拟期权交易软件 编辑:程序博客网 时间:2024/05/16 10:07

//---------------------------通信录操作录工作开始--------------------------------------------------

//新增联系人

-(void)AddPeople

{

    //取得本地通信录名柄

   ABAddressBookRef tmpAddressBook = ABAddressBookCreate();

    //创建一条联系人记录

   ABRecordRef tmpRecord = ABPersonCreate();

   CFErrorRef error;

   BOOL tmpSuccess = NO;

    //Nickname

   CFStringRef tmpNickname = CFSTR("Sparky");

    tmpSuccess =ABRecordSetValue(tmpRecord, kABPersonNicknameProperty, tmpNickname, &error);

   CFRelease(tmpNickname);

    //First name

   CFStringRef tmpFirstName = CFSTR("zhang");

    tmpSuccess =ABRecordSetValue(tmpRecord, kABPersonFirstNameProperty, tmpFirstName, &error);

   CFRelease(tmpFirstName);

    //Last name

   CFStringRef tmpLastName = CFSTR("shan");

    tmpSuccess =ABRecordSetValue(tmpRecord, kABPersonLastNameProperty, tmpLastName, &error);

   CFRelease(tmpLastName);

    //phone number

   CFTypeRef tmpPhones = CFSTR("13902400000");

    ABMutableMultiValueRef tmpMutableMultiPhones =ABMultiValueCreateMutable(kABPersonPhoneProperty);

   ABMultiValueAddValueAndLabel(tmpMutableMultiPhones, tmpPhones,kABPersonPhoneMobileLabel, NULL);

    tmpSuccess =ABRecordSetValue(tmpRecord, kABPersonPhoneProperty, tmpMutableMultiPhones, &error);

   CFRelease(tmpPhones);

    //保存记录

    tmpSuccess =ABAddressBookAddRecord(tmpAddressBook, tmpRecord, &error);

   CFRelease(tmpRecord);

    //保存数据库

    tmpSuccess =ABAddressBookSave(tmpAddressBook, &error);

   CFRelease(tmpAddressBook);

}


//删除联系人

-(void)DeletePeople

{

    //取得本地通信录名柄

   ABAddressBookRef tmpAddressBook = ABAddressBookCreate();

   NSArray* tmpPersonArray = (NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);

   for(id tmpPersonin tmpPersonArray)

    {

       NSString* tmpFirstName = (NSString*)ABRecordCopyValue(tmpPerson,kABPersonFirstNameProperty);

       NSString* tmpLastName  = (NSString*)ABRecordCopyValue(tmpPerson,kABPersonLastNameProperty);

       NSString* tmpFullName = [NSStringstringWithFormat: @"%@%@", [tmpFirstName lowercaseString], [tmpLastNamelowercaseString]];

        [tmpFirstNamerelease];

        [tmpLastNamerelease];

       //删除联系人

       if([tmpFullName isEqualToString:@"zhangshan"])

        {

           ABAddressBookRemoveRecord(tmpAddressBook, tmpPerson,nil);

        }

    }

    //保存电话本

   ABAddressBookSave(tmpAddressBook, nil);

    //释放内存

    [tmpPersonArrayrelease];

   CFRelease(tmpAddressBook);

}


//读取所有联系人

-(void)readAllPeoples:(ABAddressBookRef) tmpAddressBook

{

    //取得本地通信录名柄

    //    ABAddressBookRef tmpAddressBook = ABAddressBookCreate();

    

    //取得本地所有联系人记录

   NSArray* tmpPeoples = (NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);

  

    

    

   for(id tmpPersonin tmpPeoples)

    {

        //获取的联系人单一属性:First name

       NSString* tmpFirstName = (NSString*)ABRecordCopyValue(tmpPerson,kABPersonFirstNameProperty);

       NSLog(@"First name:%@", tmpFirstName);

        [tmpFirstNamerelease];

        //获取的联系人单一属性:Last name

       NSString* tmpLastName = (NSString*)ABRecordCopyValue(tmpPerson,kABPersonLastNameProperty);

       NSLog(@"Last name:%@", tmpLastName);

        [tmpLastNamerelease];

        //获取的联系人单一属性:Nickname

       NSString* tmpNickname = (NSString*)ABRecordCopyValue(tmpPerson,kABPersonNicknameProperty);

       NSLog(@"Nickname:%@", tmpNickname);

        [tmpNicknamerelease];

        //获取的联系人单一属性:Company name

        NSString* tmpCompanyname = (NSString*)ABRecordCopyValue(tmpPerson,kABPersonOrganizationProperty);

       NSLog(@"Company name:%@", tmpCompanyname);

        [tmpCompanynamerelease];

        //获取的联系人单一属性:Job Title

        NSString* tmpJobTitle= (NSString*)ABRecordCopyValue(tmpPerson,kABPersonJobTitleProperty);

       NSLog(@"Job Title:%@", tmpJobTitle);

        [tmpJobTitlerelease];

        //获取的联系人单一属性:Department name

       NSString* tmpDepartmentName = (NSString*)ABRecordCopyValue(tmpPerson,kABPersonDepartmentProperty);

       NSLog(@"Department name:%@", tmpDepartmentName);

        [tmpDepartmentNamerelease];

        //获取的联系人单一属性:Email(s)

        ABMultiValueRef tmpEmails =ABRecordCopyValue(tmpPerson,kABPersonEmailProperty);

       for(NSInteger j =0; ABMultiValueGetCount(tmpEmails); j++)

        {

           NSString* tmpEmailIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpEmails, j);

           NSLog(@"Emails%d:%@", j, tmpEmailIndex);

            [tmpEmailIndexrelease];

        }

       CFRelease(tmpEmails);

        //获取的联系人单一属性:Birthday

       NSDate* tmpBirthday = (NSDate*)ABRecordCopyValue(tmpPerson,kABPersonBirthdayProperty);

       NSLog(@"Birthday:%@", tmpBirthday);

        [tmpBirthdayrelease];

        //获取的联系人单一属性:Note

       NSString* tmpNote = (NSString*)ABRecordCopyValue(tmpPerson,kABPersonNoteProperty);

       NSLog(@"Note:%@", tmpNote);

        [tmpNoterelease];

        //获取的联系人单一属性:Generic phone number

        ABMultiValueRef tmpPhones =ABRecordCopyValue(tmpPerson,kABPersonPhoneProperty);

       for(NSInteger j =0; j < ABMultiValueGetCount(tmpPhones); j++)

        {

           NSString* tmpPhoneIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j);

           NSLog(@"tmpPhoneIndex%d:%@", j, tmpPhoneIndex);

            [tmpPhoneIndexrelease];

        }

       CFRelease(tmpPhones);

    }

    

    //释放内存

    [tmpPeoplesrelease];

    

   CFRelease(tmpAddressBook);

}


-(void)allContactPerson{

    

   ABAddressBookRef addressBook = nil;

    

   if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 6.0)

    {

        addressBook = ABAddressBookCreateWithOptions(NULL,NULL);

        

        //等待同意后向下执行

        dispatch_semaphore_t sema =dispatch_semaphore_create(0);

       ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted,CFErrorRef error)

                                                 {

                                                    dispatch_semaphore_signal(sema);

                                                 });

        

        dispatch_semaphore_wait(sema,DISPATCH_TIME_FOREVER);

        dispatch_release(sema);

    }

   else

    {

        addressBook =ABAddressBookCreate();

    }

    

    //   NSArray *personArray = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    

    [selfreadAllPeoples:addressBook];

}


//---------------------------通讯录操作结束--------------


原创粉丝点击