iOS開發——高級技術&通信錄功能的實現

通信錄功能的實現

iOS 提供了對通信錄操做的組建,其中一個是直接操做通信錄,另外一個是調用通信錄的 UI 組建。實現方法以下:javascript

    添加AddressBook.framework到工程中。java

01_JPG_thumb.jpg

    代碼實現:ios

  

複製代碼
 1  -(IBAction)onClickbutton:(id)sender  2  {  3 NSMutableArray* personArray = [[[NSMutableArray alloc] init] autorelease];  4 ABAddressBookRef addressBook = ABAddressBookCreate();  5 NSString *firstName, *lastName, *fullName;  6 personArray = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);  7 if ([sender tag]==0) {  8  9 for (id *person in personArray) 10  { 11 firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 12 firstName = [firstName stringByAppendingFormat:@" "]; 13 lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 14 fullName = [firstName stringByAppendingFormat:@"%@",lastName]; 15 NSLog(@"===%@",fullName); 16 ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty); 17 for(int i = 0 ;i < ABMultiValueGetCount(phones); i++) 18  { 19 NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i); 20 NSLog(@"===%@",phone); 21  } 22 ABMultiValueRef mails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty); 23 for(int i = 0 ;i < ABMultiValueGetCount(mails); i++) 24  { 25 NSString *mail = (NSString *)ABMultiValueCopyValueAtIndex(mails, i); 26 NSLog(@"==%@",mail); 27  } 28  } 29 }else { 30 //刪除信息 31 //返回全部聯繫人到一個數組中 32 CFArrayRef personArray = ABAddressBookCopyArrayOfAllPeople(addressBook); 33 CFIndex personCount = ABAddressBookGetPersonCount(addressBook); 34 for (int i =0;i<personCount;i++){ 35 ABRecordRef ref = CFArrayGetValueAtIndex(personArray, i); 36 CFStringRef firstName1 = ABRecordCopyValue(ref, kABPersonFirstNameProperty); 37 CFStringRef lastName1 = ABRecordCopyValue(ref, kABPersonLastNameProperty); 38 NSString *contactFirstLast = [NSString stringWithFormat: @"%@%@", (NSString *)firstName1,(NSString *)lastName1]; 39 if ([contactFirstLast isEqualToString:@"徐夢"]) { 40 //刪除聯繫人 41 ABAddressBookRemoveRecord(addressBook, ref, nil); 42  } 43  } 44 //保存電話本 45  ABAddressBookSave(addressBook, nil); 46 //釋放內存 47 //CFRelease(personRef); 48 // CFRelease(addressbookRef); 49  } 50 }
複製代碼

 

 

完整實現(一個項目要求app能讀取手機的通信錄)數組

 

其實我是反對這類的需求,你說你讀個人隱私,我確定不肯意的。幸虧ios6.0 之後給了個權限控制。當打開app的時候你能夠選擇拒絕。

 

 

 

 

 

實現方法:

 

複製代碼
  1 //讀取全部聯繫人  2  3 -(void)ReadAllPeoples  4  5 {  6  7 //取得本地通訊錄名柄  8  9 ABAddressBookRef tmpAddressBook = nil;  10  11 if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) {  12 tmpAddressBook=ABAddressBookCreateWithOptions(NULL, NULL);  13 dispatch_semaphore_t sema=dispatch_semaphore_create(0);  14 ABAddressBookRequestAccessWithCompletion(tmpAddressBook, ^(bool greanted, CFErrorRef error){  15  dispatch_semaphore_signal(sema);  16  });  17  18  dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  19  dispatch_release(sema);  20  }  21 else  22  {  23 tmpAddressBook =ABAddressBookCreate();  24  }  25 //取得本地全部聯繫人記錄  26  27  28 if (tmpAddressBook==nil) {  29 return ;  30  };  31 NSArray* tmpPeoples = (NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);  32  33 for(id tmpPerson in tmpPeoples)  34  35  {  36  37 //獲取的聯繫人單一屬性:First name  38  39 NSString* tmpFirstName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonFirstNameProperty);  40  41 NSLog(@"First name:%@", tmpFirstName);  42  43  [tmpFirstName release];  44  45 //獲取的聯繫人單一屬性:Last name  46  47 NSString* tmpLastName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonLastNameProperty);  48  49 NSLog(@"Last name:%@", tmpLastName);  50  51  [tmpLastName release];  52  53 //獲取的聯繫人單一屬性:Nickname  54  55 NSString* tmpNickname = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonNicknameProperty);  56  57 NSLog(@"Nickname:%@", tmpNickname);  58  59  [tmpNickname release];  60  61 //獲取的聯繫人單一屬性:Company name  62  63 NSString* tmpCompanyname = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonOrganizationProperty);  64  65 NSLog(@"Company name:%@", tmpCompanyname);  66  67  [tmpCompanyname release];  68  69 //獲取的聯繫人單一屬性:Job Title  70  71 NSString* tmpJobTitle= (NSString*)ABRecordCopyValue(tmpPerson, kABPersonJobTitleProperty);  72  73 NSLog(@"Job Title:%@", tmpJobTitle);  74  75  [tmpJobTitle release];  76  77 //獲取的聯繫人單一屬性:Department name  78  79 NSString* tmpDepartmentName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonDepartmentProperty);  80  81 NSLog(@"Department name:%@", tmpDepartmentName);  82  83  [tmpDepartmentName release];  84  85 //獲取的聯繫人單一屬性:Email(s)  86  87 ABMultiValueRef tmpEmails = ABRecordCopyValue(tmpPerson, kABPersonEmailProperty);  88  89 for(NSInteger j = 0; ABMultiValueGetCount(tmpEmails); j++)  90  91  {  92  93 NSString* tmpEmailIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpEmails, j);  94  95 NSLog(@"Emails%d:%@", j, tmpEmailIndex);  96  97  [tmpEmailIndex release];  98  99  } 100 101  CFRelease(tmpEmails); 102 103 //獲取的聯繫人單一屬性:Birthday 104 105 NSDate* tmpBirthday = (NSDate*)ABRecordCopyValue(tmpPerson, kABPersonBirthdayProperty); 106 107 NSLog(@"Birthday:%@", tmpBirthday); 108 109  [tmpBirthday release]; 110 111 //獲取的聯繫人單一屬性:Note 112 113 NSString* tmpNote = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonNoteProperty); 114 115 NSLog(@"Note:%@", tmpNote); 116 117  [tmpNote release]; 118 119 //獲取的聯繫人單一屬性:Generic phone number 120 121 ABMultiValueRef tmpPhones = ABRecordCopyValue(tmpPerson, kABPersonPhoneProperty); 122 123 for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++) 124 125  { 126 127 NSString* tmpPhoneIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j); 128 129 NSLog(@"tmpPhoneIndex%d:%@", j, tmpPhoneIndex); 130 131  [tmpPhoneIndex release]; 132 133  } 134 135  CFRelease(tmpPhones); 136 137  } 138 139 //釋放內存&nbsp; 140 141  [tmpPeoples release]; 142 143  CFRelease(tmpAddressBook); 144 145 }
複製代碼

 

解釋下代碼:因爲ios6.0系統的升級,對客戶隱私有了一個較大的提示。

 

之前
1 tmpAddressBook =ABAddressBookCreate();這樣就能夠讀取了,可是如今要作一個判斷了。能夠具體看看代碼。
  1. 把通信錄取出來放在一個array裏面。這樣你就能夠對數組進行你的要求了。 
 

備註:

蘋果中文語言貌似有點問題。ios6.0  會提示你的app訪問日曆。。。。
而後網上查了下資料。能夠參照微信訪問通信錄的形式,給客戶一個提示
他是在plist中  添加一項 在XCode裏顯示的是Privacy - Contacts Usage Description。後面的string就是你要想提示的文字內容
Privacy - Location Usage Description」。這個是定位位置提示。其餘相似。

備註二:

分享一個測試經驗,也是從網上看到的。
一把蘋果的app訪問位置或者通信錄,第一次都會有一個提示讓你選擇。
可是第二次及之後,即時刪掉,也不會再出現了。
若是想再次出現,能夠  設置-通用-還原-還原位置與隱私
這樣你再次點擊你的app就會再次出現了。
相關文章
相關標籤/搜索