zoukankan      html  css  js  c++  java
  • iOS有关通讯录操作

    一、首先获取用户通讯录授权信息。

    在AppDelegate中导入#import <AddressBook/AddressBook.h>框架,在下列方法中获取授权信息。
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    具体代码:

     1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     2     
     3     // 1.获取通讯录授权状态
     4     ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
     5     // 2.授权申请
     6     if (status == kABAuthorizationStatusNotDetermined) {
     7         // 有create就一定有release
     8         ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
     9         
    10         ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) {
    11             if (granted) {
    12                 NSLog(@"授权允许");
    13             }else {
    14                 NSLog(@"授权拒绝");
    15             }
    16         });
    17         
    18         CFRelease(book);
    19     }
    20     return YES;
    21 }

    二、对通讯录联系人属性进行的一系列操作

      1 - (void)viewDidLoad {
      2     [super viewDidLoad];
      3     // 1.创建通讯录
      4     ABAddressBookRef book = ABAddressBookCreate();
      5     // 2.得到所有通讯录
      6     CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(book);
      7     
      8     for (NSUInteger i=0; i<CFArrayGetCount(results); i++) {
      9         ABRecordRef person = CFArrayGetValueAtIndex(results, i);
     10         
     11         // 读取firstName
     12         NSString *personName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
     13         if (personName != nil) {
     14             NSLog(@"名:%@", personName);
     15         }
     16         
     17         // 获取lastName
     18         NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
     19         if (lastName != nil) {
     20             NSLog(@"姓:%@", lastName);
     21         }
     22         
     23 //        NSString *lastNamePhonetic = (__bridge NSString *)(ABRecordCopyValue(book, kABPersonLastNamePhoneticProperty));
     24 //        if (lastNamePhonetic != nil) {
     25 //            NSLog(@"%@", lastNamePhonetic);
     26 //        }
     27         //读取organization公司
     28         NSString *organization = (__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
     29         if(organization != nil) NSLog(@"%@", organization);
     30         
     31         
     32         //获取email多值
     33         ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
     34         int emailcount = ABMultiValueGetCount(email);
     35         for (int x = 0; x < emailcount; x++)
     36         {
     37             //获取email Label
     38             NSString* emailLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));
     39             //获取email值
     40             NSString* emailContent = (__bridge NSString*)ABMultiValueCopyValueAtIndex(email, x);
     41             NSLog(@"emailLabel:%@,emailContent:%@",emailLabel,emailContent);
     42         }
     43         //读取地址多值
     44         ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
     45         int count = ABMultiValueGetCount(address);
     46         
     47         for(int j = 0; j < count; j++)
     48         {
     49             //获取地址Label
     50             NSString* addressLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(address, j);
     51             NSLog(@"%@",addressLabel);
     52             //获取該label下的地址6属性
     53             NSDictionary* personaddress =(__bridge NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);
     54             NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
     55             if(country != nil)
     56                 NSLog(@"国家:%@
    ",country);
     57             NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
     58             if(city != nil)
     59                 NSLog(@"城市:%@
    ",city);
     60             NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
     61             if(state != nil)
     62                 NSLog(@"省:%@
    ",state);
     63             NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
     64             if(street != nil)
     65                 NSLog(@"街道:%@
    ",street);
     66             NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];
     67             if(zip != nil)
     68                 NSLog(@"邮编:%@
    ",zip);
     69             NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];
     70             if(coutntrycode != nil)
     71                 NSLog(@"国家编号:%@
    ",coutntrycode);
     72         }
     73         //第一次添加该条记录的时间
     74         NSString *firstknow = (__bridge NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);
     75         NSLog(@"第一次添加该条记录的时间%@
    ",firstknow);
     76         //最后一次修改該条记录的时间
     77         NSString *lastknow = (__bridge NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);
     78         NSLog(@"最后一次修改该条记录的时间%@
    ",lastknow);
     79         
     80         //读取电话多值
     81         ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
     82         for (int k = 0; k<ABMultiValueGetCount(phone); k++)
     83         {
     84             //获取电话Label
     85             NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
     86             //获取該Label下的电话值
     87             NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);
     88             
     89             NSLog(@"%@:%@
    ",personPhoneLabel,personPhone);
     90         }
     91         
     92         //获取URL多值
     93         ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);
     94         for (int m = 0; m < ABMultiValueGetCount(url); m++)
     95         {
     96             //获取电话Label
     97             NSString * urlLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));
     98             //获取該Label下的电话值
     99             NSString * urlContent = (__bridge NSString*)ABMultiValueCopyValueAtIndex(url,m);
    100             
    101             NSLog(@"%@:%@
    ",urlLabel,urlContent);
    102         }
    103         
    104         //读取照片
    105         NSData *image = (__bridge NSData*)ABPersonCopyImageData(person);
    106         
    107         
    108         UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
    109         [myImage setImage:[UIImage imageWithData:image]];
    110         myImage.opaque = YES;
    111         [self.view addSubview:myImage];
    112         
    113     }
    114     
    115     CFRelease(book);
    116     CFRelease(results);
    117 }

    这里只是获取了通讯录联系人的一部分属性。获取更多的属性参考《iOS 获取通讯录中联系人的所有属性

    三、获取这些属性你也可以利用三方框架RHAddressBook 参考: ios中访问通讯录数据

    引入头文件,这里我用的是cocopods管理这个三方框架,引入框架同时 #import <RHAddressBook.h>,还要引入 #import <RHAddressBook/RHPerson.h>这个头文件。具体代码:

     1  // 创建通讯录对象
     2     RHAddressBook *book = [[RHAddressBook alloc] init];
     3     // 获取通讯录所有联系人
     4     NSArray *peopleArray = book.people;
     5     
     6     for (RHPerson *people in peopleArray) {
     7     
     8         //获取人员的firstName
     9         NSString* firstName = people.firstName;
    10         //获取人员的lastName
    11         NSString* lastName = people.lastName;
    12         //获取该人员的号码(号码有多个,所以用RHMultiValue)
    13         RHMultiValue* phoneNumbers = people.phoneNumbers;
    14         NSUInteger phoneNumberCount = phoneNumbers.count;
    15         for (int i = 0; i < phoneNumberCount; i++) {
    16             //遍历每个号码中的label(比如:手机 家庭 公司)
    17             NSString* label = [phoneNumbers labelAtIndex:i];
    18             //遍历出号码
    19             NSString* nember = [phoneNumbers valueAtIndex:i];
    20             NSLog(@"%@, %@ ,%@,%@",firstName,lastName,label,nember);
    21         }
    22         NSLog(@"%@", people);
    23         
    24     }

    当然还有很多属性,具体可以进入文件查看其属性。

    四、如果你对通讯录的添加删除感兴趣,可以参考 

    《iOS添加、删除通讯录》
     1 - (void)addAddressBook
     2 {
     3     // 创建一个通讯录操作对象
     4     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
     5     ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
     6         if (granted) {
     7             // 创建新的联系人记录
     8             ABRecordRef newRecord = ABPersonCreate();
     9             NSString *firstName = @"";
    10             NSString *lastName = @"";
    11             // 为新的联系人添加属性值
    12             ABRecordSetValue(newRecord, kABPersonFirstNameProperty, (__bridge CFTypeRef)(firstName), NULL);
    13             ABRecordSetValue(newRecord, kABPersonLastNameProperty, (__bridge CFTypeRef)(lastName), NULL);
    14             
    15             // 创建一个多值属性
    16             ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    17             
    18             NSString *mobeileLabel = @"155";
    19             ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)(mobeileLabel), kABPersonPhoneMobileLabel, NULL);
    20            
    21             // 将多值属性添加到记录
    22             ABRecordSetValue(newRecord, kABPersonPhoneProperty, multi, NULL);
    23             
    24             // 添加记录到通讯录操作对象
    25             ABAddressBookAddRecord(addressBook, newRecord, NULL);
    26             
    27             CFRelease(multi);
    28             CFRelease(newRecord);
    29         }
    30     });
    31     ABAddressBookSave(addressBook, NULL);
    32     CFRelease(addressBook);
    33 }

    五、推荐一篇值得学习的文章  iOS开发——高级篇——通讯录

     
  • 相关阅读:
    javascript得知
    一旦配置oracle em经验
    windows 下一个mysql password忘记改变
    彩色图像--图像切 色彩空间切割
    Delphi 6 Web Services初步评估之三(转)
    Delphi 用Web App Debugger简单调试ISAPI 转
    Delphi socket() 函数的应用
    用Delphi实现WinSocket高级应用
    delphi编写winsocket的流程
    Delphi使用NativeXml访问XML文件
  • 原文地址:https://www.cnblogs.com/peaker-wu/p/5305200.html
Copyright © 2011-2022 走看看