iOS7 iOS8 调用系统通讯录界面

来源:互联网 发布:淘宝上买炉石卡包 编辑:程序博客网 时间:2024/05/16 03:42

#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 = [[UIAlertViewalloc]initWithTitle:@""message:@"此应用程序没有权限访问您的联系人。您可以在'设置'->'隐私'->'通讯录'->'健一网'中启用访问权限"delegate:selfcancelButtonTitle:@"知道了"otherButtonTitles:nil,nil];

        [alart show];

    }else{

      _peoplePicker= [[ABPeoplePickerNavigationControlleralloc] init];

        _peoplePicker.peoplePickerDelegate =self;

         NSArray *displayItems = [NSArrayarrayWithObjects:[NSNumbernumberWithInt:kABPersonPhoneProperty],nil];

        _peoplePicker.displayedProperties=displayItems;

//        if(IOS8){

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

//        }

        [selfpresentViewController:_peoplePickeranimated:YEScompletion: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(@"%@",[NSStringstringWithFormat:@"%@%@",firstName,lastName]);

    //     CFRelease(person);

    CFIndex index =ABMultiValueGetIndexForIdentifier(valuesRef,identifier);

    if(property !=kABPersonPhoneProperty) {

       return;

    }else{

        CFStringRef value =ABMultiValueCopyValueAtIndex(valuesRef,index);

        [selfdismissViewControllerAnimated:YEScompletion:^{

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

            selectNumber=[NSStringstringWithFormat:@"%@",(__bridgeNSString*)value];

            if ([selectNumberisEqualToString:@"(null)"]) {

                selectNumber=@"";

            }else{

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

                

                selectNumber=[HYShareObjectformatPhoneNum:selectNumber];

            }

            [self.tablereloadData];

        }];

    }

   


}

//ios7执行

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

    if(property !=kABPersonPhoneProperty) {

        returnNO;

    }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(@"%@",[NSStringstringWithFormat:@"%@%@",firstName,lastName]);

        //     CFRelease(person);

        CFIndex index =ABMultiValueGetIndexForIdentifier(valuesRef,identifier);

      

            CFStringRef value =ABMultiValueCopyValueAtIndex(valuesRef,index);

            [selfdismissViewControllerAnimated:YEScompletion:^{

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

                selectNumber=[NSStringstringWithFormat:@"%@",(__bridgeNSString*)value];

                if ([selectNumberisEqualToString:@"(null)"]) {

                    selectNumber=@"";

                }else{

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

                    

                    selectNumber=[HYShareObjectformatPhoneNum:selectNumber];

                }

                [self.tablereloadData];

            }];

    

    }

    returnYES;

   

}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker


{

    [selfdismissViewControllerAnimated:YEScompletion:nil];

    

 

    

}


0 0
原创粉丝点击