iOS7 iOS8 调用系统通讯录界面

#pragma mark--选取手机联系人

-(void)selectPeople{

    //这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录

    int __block tip=0;

    //声明一个通讯簿的引用

    ABAddressBookRef addBook =nil;

        //创建通讯簿的引用

        addBook=ABAddressBookCreateWithOptions(NULL, NULL);

        //创建一个出事信号量为0的信号

        dispatch_semaphore_t sema=dispatch_semaphore_create(0);

        //申请访问权限

        ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted, CFErrorRef error)        {

            //greantedYES是表示用户允许,否则为不允许

            if (!greanted) {

                tip=1;

            }

            //发送一次信号

            dispatch_semaphore_signal(sema);

        });

        //等待信号触发

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    

    

    

    if (tip) {

        //做一个友好的提示

        UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"" message:@"此应用程序没有权限访问您的联系人。您可以在'设置'->'隐私'->'通讯录'->'健一网'中启用访问权限" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

        [alart show];

    }else{

      _peoplePicker= [[ABPeoplePickerNavigationController alloc] init];

        _peoplePicker.peoplePickerDelegate = self;

         NSArray *displayItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],nil];

        _peoplePicker.displayedProperties=displayItems;

//        if(IOS8){

//            _peoplePicker.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];

//        }

        [self presentViewController:_peoplePicker animated:YES completion:nil];

    }

}



#pragma mark -- ABPeoplePickerNavigationControllerDelegate

//ios8执行

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {


    ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);

    //查找这条记录中的名字

    NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));

    firstName = firstName != nil? firstName:@"";

    //查找这条记录中的姓氏

    NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));

      lastName = lastName != nil? lastName:@"";

    NSLog(@"%@",[NSString stringWithFormat:@"%@%@",firstName,lastName]);

    //     CFRelease(person);

    CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);

    if(property != kABPersonPhoneProperty) {

       return;

    }else{

        CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index);

        [self dismissViewControllerAnimated:YES completion:^{

            selectPeople=[NSString stringWithFormat:@"%@%@",lastName,firstName];

            selectNumber=[NSString stringWithFormat:@"%@",(__bridge NSString*)value];

            if ([selectNumber isEqualToString:@"(null)"]) {

                selectNumber=@"";

            }else{

                selectNumber=[selectNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];

                

                selectNumber=[HYShareObject formatPhoneNum:selectNumber];

            }

            [self.table reloadData];

        }];

    }

   


}

//ios7执行

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_DEPRECATED_IOS(2_0, 8_0){

    if(property != kABPersonPhoneProperty) {

        return NO;

    }else{

        ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);

        //查找这条记录中的名字

        NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));

        firstName = firstName != nil? firstName:@"";

        //查找这条记录中的姓氏

        NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));

        lastName = lastName != nil? lastName:@"";

        NSLog(@"%@",[NSString stringWithFormat:@"%@%@",firstName,lastName]);

        //     CFRelease(person);

        CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);

      

            CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index);

            [self dismissViewControllerAnimated:YES completion:^{

                selectPeople=[NSString stringWithFormat:@"%@%@",lastName,firstName];

                selectNumber=[NSString stringWithFormat:@"%@",(__bridge NSString*)value];

                if ([selectNumber isEqualToString:@"(null)"]) {

                    selectNumber=@"";

                }else{

                    selectNumber=[selectNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];

                    

                    selectNumber=[HYShareObject formatPhoneNum:selectNumber];

                }

                [self.table reloadData];

            }];

    

    }

    return YES;

   

}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker


{

    [self dismissViewControllerAnimated:YES completion:nil];

    

 

    

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值