- (void)requestAuthorizationForAddressBook {
? ? ? ? // 判斷當(dāng)前的授權(quán)狀態(tài)是否是用戶還未選擇的狀態(tài)
? ? ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
? ? if (status == kABAuthorizationStatusNotDetermined)
? ? {
? ? ? ? ABAddressBookRefbookRef =ABAddressBookCreate();
? ? ? ? ABAddressBookRequestAccessWithCompletion(bookRef, ^(bool granted, CFErrorRef error) {
? ? ? ? ? ? if(granted)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? NSLog(@"授權(quán)成功!");
? ? ? ? ? ? ? ? [selfgetmyAddressbook];
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? NSLog(@"授權(quán)失敗!");
? ? ? ? ? ? }
? ? ? ? });
? ? }else if (status == kABAuthorizationStatusAuthorized){
? ? ? ? [self getmyAddressbook];
? ? }else{
? ? ? ? UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:nilmessage:@"您的通訊錄暫未允許訪問,請(qǐng)去設(shè)置->隱私里面授權(quán)!"delegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil,nil];
? ? ? ? [alertshow];
? ? ? ? NSLog(@"您的通訊錄暫未允許訪問宇植,請(qǐng)去設(shè)置->隱私里面授權(quán)!");
? ? ? ? return;
? ? }
}
- (void)getmyAddressbook {
? ? // 創(chuàng)建通訊錄對(duì)象
? ? ABAddressBookRef bookRef = ABAddressBookCreate();
? ? // 獲取通訊錄中所有的聯(lián)系人
? ? CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(bookRef);
? ? ? ? // 遍歷所有聯(lián)系人
? ? CFIndexcount =CFArrayGetCount(arrayRef);
? ? for(inti =0; i < count; i++)
? ? {
? ? ? ? ABRecordRefrecord =CFArrayGetValueAtIndex(arrayRef, i);
? ? ? ? AddressBookData * data = [[AddressBookData alloc] init];
? ? ? ? // 獲取姓名
? ? ? ? NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
? ? ? ? NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
? ? ? ? NSLog(@"firstName = %@, lastName = %@", firstName, lastName);
? ? ? ? // 獲取電話號(hào)碼
? ? ? ? ABMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonPhoneProperty);
? ? ? ? CFIndexcount =ABMultiValueGetCount(multiValue);
? ? ? ? NSMutableArray * array = [[NSMutableArray alloc] init];
? ? ? ? for(inti =0; i < count; i ++)
? ? ? ? {
? ? ? ? ? ? NSString*label = (__bridge_transferNSString*)ABMultiValueCopyLabelAtIndex(multiValue, i);
? ? ? ? ? ? NSString*phone = (__bridge_transferNSString*)ABMultiValueCopyValueAtIndex(multiValue, i);
? ? ? ? ? ? NSLog(@"label = %@, phone = %@", label, phone);
? ? ? ? ? ? NSString *strUrl = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""];? //去掉-
? ? ? ? ? ? [arrayaddObject:strUrl];
? ? ? ? }
? ? ? ? data.userMobileArray= [arraycopy];
? ? ? ? if([NSStringisEmptyString:firstName] && [NSStringisEmptyString:lastName]) {
? ? ? ? ? ? data.userNameStr= [arrayfirstObject];
? ? ? ? }elseif(![NSStringisEmptyString:firstName] && [NSStringisEmptyString:lastName]){
? ? ? ? ? ? data.userNameStr= firstName;
? ? ? ? }
? ? ? ? elseif([NSStringisEmptyString:firstName] && ![NSStringisEmptyString:lastName]){
? ? ? ? ? ? data.userNameStr= lastName;
? ? ? ? }else{
? ? ? ? ? ? data.userNameStr= [NSStringstringWithFormat:@"%@%@",lastName,firstName];
? ? ? ? }
? ? ? ? [self.listArrayaddObject:data];
? ? ? ? [self.tableViewreloadData];
? ? ? ? CFRelease(multiValue);
? ? }
? ? CFRelease(bookRef);
? ? CFRelease(arrayRef);
}