| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- //
- // ContactViewController.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/7/29.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "ContactViewController.h"
- #import <AddressBook/AddressBook.h>
- #import "SVProgressHUD.h"
- #import "UIView+Toast.h"
- #import "HttpRequest.h"
- #import "ContactTableViewCell.h"
- #import "User.h"
- #import "UIView+NTES.h"
- @implementation ContactData
- @end
- @interface ContactViewController ()<UISearchBarDelegate>
- @property (nonatomic, strong) NSMutableArray *contactArr;
- @property (nonatomic, strong) NSMutableDictionary *contactDict;
- @property (nonatomic, strong) UIView *authView;
- @property (nonatomic, strong) UISearchController *searchController;
- @property (nonatomic, strong) NSMutableArray *searchArr;
- @end
- @implementation ContactViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- _authView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
- [self.view addSubview:_authView];
-
- float imageWidth = self.view.width - 40 * 2;
- UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, imageWidth, imageWidth)];
- [imgView setImage:[UIImage imageNamed:@"开启通讯录_03"]];
- [_authView addSubview:imgView];
-
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- btn.frame = CGRectMake(40, imgView.bottom + (_authView.height - imgView.bottom)/2 - 25, imageWidth, 50);
- btn.layer.cornerRadius = 25;
- btn.backgroundColor = User.orangeColor;
- [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
- [btn setTitle:@"开启通讯录" forState:UIControlStateNormal];
- [btn addTarget:self action:@selector(onTouchContactAuth:) forControlEvents:UIControlEventTouchUpInside];
- [_authView addSubview:btn];
-
-
-
- _searchArr = [[NSMutableArray alloc] init];
-
- _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
- _searchController.hidesNavigationBarDuringPresentation = NO;
- _searchController.searchBar.tintColor = UIColor.whiteColor;
- _searchController.dimsBackgroundDuringPresentation = NO;
- _searchController.searchBar.delegate = self;
- [self.tableView setTableHeaderView:_searchController.searchBar];
-
- [self showContactAuth];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
- [self.navigationItem setTitle:@"手机联系人"];
- }
- - (void)viewDidAppear:(BOOL)animated{
- [super viewDidAppear:animated];
- [self requestAuthorizationAddressBook];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- [self.searchController setActive:NO];
- }
- - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
- {
- NSLog(@"textDidChange %@ %@", searchBar.text, searchText);
-
- if(searchText.length > 0)
- {
- [self.searchArr removeAllObjects];
-
- for(int i=0; i<_contactArr.count; i++)
- {
- ContactData *data = [_contactArr objectAtIndex:i];
- if([[data.name lowercaseString] containsString:[searchText lowercaseString]])
- {
- [_searchArr addObject:data];
- }
- }
- }
- else
- {
- [self.searchArr removeAllObjects];
- }
-
- [self.tableView reloadData];
- }
- - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
- {
- [self.searchArr removeAllObjects];
- [self.tableView reloadData];
- }
- - (void)requestAuthorizationAddressBook {
- // 判断是否授权
- ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
- if (authorizationStatus == kABAuthorizationStatusNotDetermined) {
- // 请求授权
- ABAddressBookRef addressBookRef = ABAddressBookCreate();
- __weak typeof(self) wself = self;
- ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
- if (granted) { // 授权成功
- [wself setUpContact];
- } else { // 授权失败
- NSLog(@"授权失败!");
- }
- });
- return;
- }
-
- [self setUpContact];
- }
- - (void)setUpContact{
- ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
- if (authorizationStatus != kABAuthorizationStatusAuthorized) {
-
- NSLog(@"没有授权");
- [self showContactAuth];
- return;
- }
-
- [self hideContactAuth];
-
- // 2. 获取所有联系人
- _contactArr = [[NSMutableArray alloc] init];
- _contactDict = [[NSMutableDictionary alloc] init];
- NSMutableArray *mobiles = [[NSMutableArray alloc] init];
-
- ABAddressBookRef addressBookRef = ABAddressBookCreate();
- CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
- long count = CFArrayGetCount(arrayRef);
- for (int i = 0; i < count; i++) {
- //获取联系人对象的引用
- ABRecordRef people = CFArrayGetValueAtIndex(arrayRef, i);
-
- //获取当前联系人名字
- NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
-
- //获取当前联系人姓氏
- NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
- NSLog(@"--------------------------------------------------");
- NSLog(@"firstName=%@, lastName=%@", firstName, lastName);
-
- if(!firstName)
- {
- firstName = @"";
- }
- if(!lastName)
- {
- lastName = @"";
- }
-
- ContactData *data = [[ContactData alloc] init];
- data.name = [NSString stringWithFormat:@"%@%@", lastName, firstName];
-
- //获取当前联系人的电话 数组
- NSMutableArray *phoneArray = [[NSMutableArray alloc] init];
- ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);
- long phoneCount = ABMultiValueGetCount(phones);
- for(int j=0; j<phoneCount; j++)
- {
- NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j);
- personPhone = [personPhone stringByReplacingOccurrencesOfString:@" " withString:@""];
- personPhone = [personPhone stringByReplacingOccurrencesOfString:@"-" withString:@""];
- personPhone = [personPhone stringByReplacingOccurrencesOfString:@"+" withString:@""];
- [phoneArray addObject:personPhone];
- [mobiles addObject:personPhone];
- [_contactDict setValue:data forKey:personPhone];
- }
-
- data.phones = phoneArray;
- if(phoneArray.count > 0)
- [_contactArr addObject:data];
- }
-
- if(mobiles.count > 0)
- {
- [self requestState:mobiles];
- }
- }
- - (void)requestState:(NSMutableArray *)mobiles{
-
- [SVProgressHUD show];
-
- __weak typeof(self) wself = self;
- [[HttpRequest shared] contactState:mobiles success:^(NSMutableArray * _Nullable stateArr) {
- [SVProgressHUD dismiss];
-
- for(int i=0; i<stateArr.count; i++)
- {
- NSMutableDictionary *state = [stateArr objectAtIndex:i];
- NSString *userId = [state objectForKey:@"id"];
- NSString *mobile = [state objectForKey:@"mobile"];
- int follow = [[state objectForKey:@"follow"] intValue];
- ContactData *contactData = [wself.contactDict objectForKey:mobile];
- if(contactData && !contactData.userId)
- {
- contactData.userId = userId;
- contactData.follow = follow;
- }
- }
-
- [[User sharedInfo] requestFollowList:^{
- [wself.tableView reloadData];
- }];
-
- } failure:^{
- [SVProgressHUD dismiss];
- [self.view makeToast:@"获取联系人信息失败,请重试" duration:2.0 position:CSToastPositionCenter];
- }];
- }
- #pragma mark - Table view data source
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if(_searchArr.count)
- return _searchArr.count;
- if(!_contactArr)
- return 0;
- return _contactArr.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Contact"];
- ContactData *data = _searchArr.count > 0 ? [_searchArr objectAtIndex:indexPath.row] : [_contactArr objectAtIndex:indexPath.row];
- [cell setData:data];
-
- return cell;
- }
- - (void)showContactAuth{
- _authView.hidden = NO;
- _searchController.searchBar.hidden = YES;
- }
- - (void)hideContactAuth{
- _authView.hidden = YES;
- _searchController.searchBar.hidden = NO;
- }
- - (void)onTouchContactAuth:(id)sender{
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
- }
- @end
|