ContactViewController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // ContactViewController.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/7/29.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "ContactViewController.h"
  9. #import <AddressBook/AddressBook.h>
  10. #import "SVProgressHUD.h"
  11. #import "UIView+Toast.h"
  12. #import "HttpRequest.h"
  13. #import "ContactTableViewCell.h"
  14. #import "User.h"
  15. #import "UIView+NTES.h"
  16. @implementation ContactData
  17. @end
  18. @interface ContactViewController ()<UISearchBarDelegate>
  19. @property (nonatomic, strong) NSMutableArray *contactArr;
  20. @property (nonatomic, strong) NSMutableDictionary *contactDict;
  21. @property (nonatomic, strong) UIView *authView;
  22. @property (nonatomic, strong) UISearchController *searchController;
  23. @property (nonatomic, strong) NSMutableArray *searchArr;
  24. @end
  25. @implementation ContactViewController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. _authView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
  29. [self.view addSubview:_authView];
  30. float imageWidth = self.view.width - 40 * 2;
  31. UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, imageWidth, imageWidth)];
  32. [imgView setImage:[UIImage imageNamed:@"开启通讯录_03"]];
  33. [_authView addSubview:imgView];
  34. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  35. btn.frame = CGRectMake(40, imgView.bottom + (_authView.height - imgView.bottom)/2 - 25, imageWidth, 50);
  36. btn.layer.cornerRadius = 25;
  37. btn.backgroundColor = User.orangeColor;
  38. [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
  39. [btn setTitle:@"开启通讯录" forState:UIControlStateNormal];
  40. [btn addTarget:self action:@selector(onTouchContactAuth:) forControlEvents:UIControlEventTouchUpInside];
  41. [_authView addSubview:btn];
  42. _searchArr = [[NSMutableArray alloc] init];
  43. _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  44. _searchController.hidesNavigationBarDuringPresentation = NO;
  45. _searchController.searchBar.tintColor = UIColor.whiteColor;
  46. _searchController.dimsBackgroundDuringPresentation = NO;
  47. _searchController.searchBar.delegate = self;
  48. [self.tableView setTableHeaderView:_searchController.searchBar];
  49. [self showContactAuth];
  50. }
  51. - (void)didReceiveMemoryWarning {
  52. [super didReceiveMemoryWarning];
  53. // Dispose of any resources that can be recreated.
  54. }
  55. - (void)viewWillAppear:(BOOL)animated{
  56. [super viewWillAppear:animated];
  57. [self.navigationItem setTitle:@"手机联系人"];
  58. }
  59. - (void)viewDidAppear:(BOOL)animated{
  60. [super viewDidAppear:animated];
  61. [self requestAuthorizationAddressBook];
  62. }
  63. - (void)viewWillDisappear:(BOOL)animated
  64. {
  65. [super viewWillDisappear:animated];
  66. [self.searchController setActive:NO];
  67. }
  68. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  69. {
  70. NSLog(@"textDidChange %@ %@", searchBar.text, searchText);
  71. if(searchText.length > 0)
  72. {
  73. [self.searchArr removeAllObjects];
  74. for(int i=0; i<_contactArr.count; i++)
  75. {
  76. ContactData *data = [_contactArr objectAtIndex:i];
  77. if([[data.name lowercaseString] containsString:[searchText lowercaseString]])
  78. {
  79. [_searchArr addObject:data];
  80. }
  81. }
  82. }
  83. else
  84. {
  85. [self.searchArr removeAllObjects];
  86. }
  87. [self.tableView reloadData];
  88. }
  89. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
  90. {
  91. [self.searchArr removeAllObjects];
  92. [self.tableView reloadData];
  93. }
  94. - (void)requestAuthorizationAddressBook {
  95. // 判断是否授权
  96. ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
  97. if (authorizationStatus == kABAuthorizationStatusNotDetermined) {
  98. // 请求授权
  99. ABAddressBookRef addressBookRef = ABAddressBookCreate();
  100. __weak typeof(self) wself = self;
  101. ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
  102. if (granted) { // 授权成功
  103. [wself setUpContact];
  104. } else { // 授权失败
  105. NSLog(@"授权失败!");
  106. }
  107. });
  108. return;
  109. }
  110. [self setUpContact];
  111. }
  112. - (void)setUpContact{
  113. ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
  114. if (authorizationStatus != kABAuthorizationStatusAuthorized) {
  115. NSLog(@"没有授权");
  116. [self showContactAuth];
  117. return;
  118. }
  119. [self hideContactAuth];
  120. // 2. 获取所有联系人
  121. _contactArr = [[NSMutableArray alloc] init];
  122. _contactDict = [[NSMutableDictionary alloc] init];
  123. NSMutableArray *mobiles = [[NSMutableArray alloc] init];
  124. ABAddressBookRef addressBookRef = ABAddressBookCreate();
  125. CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
  126. long count = CFArrayGetCount(arrayRef);
  127. for (int i = 0; i < count; i++) {
  128. //获取联系人对象的引用
  129. ABRecordRef people = CFArrayGetValueAtIndex(arrayRef, i);
  130. //获取当前联系人名字
  131. NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
  132. //获取当前联系人姓氏
  133. NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
  134. NSLog(@"--------------------------------------------------");
  135. NSLog(@"firstName=%@, lastName=%@", firstName, lastName);
  136. if(!firstName)
  137. {
  138. firstName = @"";
  139. }
  140. if(!lastName)
  141. {
  142. lastName = @"";
  143. }
  144. ContactData *data = [[ContactData alloc] init];
  145. data.name = [NSString stringWithFormat:@"%@%@", lastName, firstName];
  146. //获取当前联系人的电话 数组
  147. NSMutableArray *phoneArray = [[NSMutableArray alloc] init];
  148. ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);
  149. long phoneCount = ABMultiValueGetCount(phones);
  150. for(int j=0; j<phoneCount; j++)
  151. {
  152. NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j);
  153. personPhone = [personPhone stringByReplacingOccurrencesOfString:@" " withString:@""];
  154. personPhone = [personPhone stringByReplacingOccurrencesOfString:@"-" withString:@""];
  155. personPhone = [personPhone stringByReplacingOccurrencesOfString:@"+" withString:@""];
  156. [phoneArray addObject:personPhone];
  157. [mobiles addObject:personPhone];
  158. [_contactDict setValue:data forKey:personPhone];
  159. }
  160. data.phones = phoneArray;
  161. if(phoneArray.count > 0)
  162. [_contactArr addObject:data];
  163. }
  164. if(mobiles.count > 0)
  165. {
  166. [self requestState:mobiles];
  167. }
  168. }
  169. - (void)requestState:(NSMutableArray *)mobiles{
  170. [SVProgressHUD show];
  171. __weak typeof(self) wself = self;
  172. [[HttpRequest shared] contactState:mobiles success:^(NSMutableArray * _Nullable stateArr) {
  173. [SVProgressHUD dismiss];
  174. for(int i=0; i<stateArr.count; i++)
  175. {
  176. NSMutableDictionary *state = [stateArr objectAtIndex:i];
  177. NSString *userId = [state objectForKey:@"id"];
  178. NSString *mobile = [state objectForKey:@"mobile"];
  179. int follow = [[state objectForKey:@"follow"] intValue];
  180. ContactData *contactData = [wself.contactDict objectForKey:mobile];
  181. if(contactData && !contactData.userId)
  182. {
  183. contactData.userId = userId;
  184. contactData.follow = follow;
  185. }
  186. }
  187. [[User sharedInfo] requestFollowList:^{
  188. [wself.tableView reloadData];
  189. }];
  190. } failure:^{
  191. [SVProgressHUD dismiss];
  192. [self.view makeToast:@"获取联系人信息失败,请重试" duration:2.0 position:CSToastPositionCenter];
  193. }];
  194. }
  195. #pragma mark - Table view data source
  196. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  197. if(_searchArr.count)
  198. return _searchArr.count;
  199. if(!_contactArr)
  200. return 0;
  201. return _contactArr.count;
  202. }
  203. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  204. ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Contact"];
  205. ContactData *data = _searchArr.count > 0 ? [_searchArr objectAtIndex:indexPath.row] : [_contactArr objectAtIndex:indexPath.row];
  206. [cell setData:data];
  207. return cell;
  208. }
  209. - (void)showContactAuth{
  210. _authView.hidden = NO;
  211. _searchController.searchBar.hidden = YES;
  212. }
  213. - (void)hideContactAuth{
  214. _authView.hidden = YES;
  215. _searchController.searchBar.hidden = NO;
  216. }
  217. - (void)onTouchContactAuth:(id)sender{
  218. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
  219. }
  220. @end