zoukankan      html  css  js  c++  java
  • ios打开通讯录及点击通讯录时提取相关信息

     首先引入头文件

    #import <AddressBookUI/ABPeoplePickerNavigationController.h>

    #import <AddressBook/ABPerson.h>

    #import <AddressBookUI/ABPersonViewController.h>

    在需要打开电话本的方法中实现:

        

    - (IBAction)openPhoneBook:(UIButton *)sender {

        NSLog(@"打开电话本");

        ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];

        peoplePicker.peoplePickerDelegate = self;

        peoplePicker.delegate = self;

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

    }

    代理方法实现回调

    #pragma mark - ABPeoplePickerNavigationControllerDelegate

    - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

    {

        [peoplePicker dismissViewControllerAnimated:YES completion:^{}];

    }

     

     

    // Called after a person has been selected by the user.

    - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0)

    {

        ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);

        NSString * phone  = @"";

        if (phoneNumbers) {

            //CFIndex count = ABMultiValueGetCount(phoneNumbers);

            phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

            CFRelease(phoneNumbers);

        }

        

        NSString * firstname = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); //

        NSString * lastname = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); //

        NSString * name = [firstname==nil?@"":firstname stringByAppendingString:lastname==nil?@"":lastname];

        

        if(phone.length && [phone hasPrefix:@"+86"]){

            phone = [phone substringFromIndex:3];

        }

        

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

        self.searchTField.text = phone;

        

        

        [peoplePicker dismissViewControllerAnimated:NO completion:^{}];

    }

     

    // Deprecated, use predicateForSelectionOfPerson and/or -peoplePickerNavigationController:didSelectPerson: instead.

    - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person NS_DEPRECATED_IOS(2_0, 8_0)

    {

        ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);

        NSString * phone  = @"";

        if (phoneNumbers) {

            //CFIndex count = ABMultiValueGetCount(phoneNumbers);

            phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

            CFRelease(phoneNumbers);

        }

        NSString * firstname = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); //

        NSString * lastname  = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); //

        NSString * name = [firstname==nil?@"":firstname stringByAppendingString:lastname==nil?@"":lastname];

        

        NSLog(@"phone=%@",phone);

        

        if(phone.length && [phone hasPrefix:@"+86"]){

            phone = [phone substringFromIndex:3];

        }

        

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

        

        self.searchTField.text = phone;

            

        

        [peoplePicker dismissViewControllerAnimated:YES completion:^{}];

        return NO;

    }

  • 相关阅读:
    【转】MySQL innodb_autoinc_lock_mode 详解 ,并发插入时主键冲突的解决方案
    optimize table在优化mysql时很重要
    Spring MVC+Mybatis 多数据源配置及发现的几个问题
    Mysql主键一致时,可以进行在元数据上的操作
    同一条sql在mysql5.6和5.7版本遇到的问题。
    实现ApplicationContextAware接口时,获取ApplicationContext为null
    获取访问者的IP
    for...in...
    CSS hack
    Base64
  • 原文地址:https://www.cnblogs.com/h-tao/p/4968741.html
Copyright © 2011-2022 走看看