通讯录AddressBook

苹果自带的通讯录

//导入通讯录的库头文件
//AddressBook提供了通讯录的所有数据
#import <AddressBook/AddressBook.h>
//AddressBookUI提供了通讯录的界面,基于AddressBook
#import <AddressBookUI/AddressBookUI.h>

- (IBAction)showAddressBookClick:(UIButton *)sender
{
    //ABPeoplePickerNavigationController : UINavigationController
    //ABPeoplePickerNavigationController  联系人选择器;  创建时不需要指定根视图控制器
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc]init];
    //在通讯录弹出present之前设置代理;  peoplePickerDelegate 联系人选择器代理,设置代理就可以得到反馈
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
    [picker release];
}

//iOS8(Xcode6)之后废弃不用

//用户点击cancel按钮时调用
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    NSLog(@"点击了取消按钮");
    [self dismissViewControllerAnimated:peoplePicker completion:nil];
}

//用户点击某个联系人时是否进入下一页
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    NSLog(@"选择了联系人");
    return YES;
}

//用户选择了某个联系人的某个资料时调用
//参数person  表示选择的联系人,这个参数中包含了这个联系人的所有资料
//参数property  表示用户选择的资料类型(电话、生日、地址等)
//参数identifier  表示用户选择的资料的索引值(例如:选择了第几个电话)
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    NSLog(@"选择了电话或者其他资料");
    //ABRecordCopyCompositeName 获得联系人的姓名
    //CFStringRef 一个结构体,是NSString的属性部分,在非ARC下可以通过强制转换;ARC下用(__bridge)进行桥接转换
    NSString *name = (NSString *)ABRecordCopyCompositeName(person);
    _nameLabel.text = name;
    CFRelease(name);

    //获取电话
    //如果用户选择了电话,再往下执行
    if (property == kABPersonPhoneProperty)
    {
        //ABRecordCopyValue 获得用户所选择的资料集合(电话、生日等)
        ABMultiValueRef ref = ABRecordCopyValue(person, property);
        //ABMultiValueGetIndexForIdentifier  获得用户所选择的资料的索引
        unsigned long index = ABMultiValueGetIndexForIdentifier(ref, identifier);
        //ABMultiValueCopyValueAtIndex  根据索引,从资料集合中获得用户所选择的资料
        NSString *phone = ABMultiValueCopyValueAtIndex(ref, index);
        //处理一下电话号码,让其为纯数字
        _phoneLabel.text = [self handlePhoneString:phone];
        CFRelease(phone);
        CFRelease(ref);
    }
    return YES;
}

//判断电话号码是否为纯数字
- (NSString *)handlePhoneString:(NSString *)phonestring
{
    NSMutableString *mString = [NSMutableString stringWithString:phonestring];
    //containsString  判断一个字符串是否包含另一个字符串
    //[@"1234567890" containsString:string];
    
    //NSCharacterSet 字符集
    //characterSetWithCharactersInString  生成一个数字字符集
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
    //遍历数组、字符串中元素,倒着遍历,正着遍历当连着几个非法字符时,会隔过去一些
    //或者在得到一个非法字符删除后将其i值-1
    //i--;
    for (NSInteger i = mString.length - 1; i>=0; i--)
    {
        //substringWithRange截取字符串中的某一部分
        //NSMakeRange(i, 1) 从第i个位置,取1个
        NSString *string = [mString substringWithRange:NSMakeRange(i, 1)];
        //        NSRange NSMakeRange (
        //                             NSUInteger loc,
        //                             NSUInteger len
        //                             );
        //        loc 为location缩写,len 表示长度。作用:在loc指定得位置开始往后获取len长度个得元素。
        
        //输入字符的字符集
        NSCharacterSet *chSet = [NSCharacterSet characterSetWithCharactersInString:string];
        //isSupersetOfSet  判断输入字符的集合是否是数字字符集的子集
        if ([set isSupersetOfSet:chSet])
        {
            //是数字
        }
        else
        {
            //不是数字 , 将其删除
            [mString deleteCharactersInRange:NSMakeRange(i, 1)];
            //或者在得到一个非法字符删除后将其i值-1
            //i--;
        }
    }
    return mString;
}
效果如下:

通讯录AddressBook_第1张图片

你可能感兴趣的:(AddressBook)