单击联系人按钮而不是设备的默认视图时,在自定义表viewcontoller中显示iOS设备中的所有联系人 [英] Display all contacts from the iOS device in custom table viewcontoller when contacts button is clicked rather than default view of the device

查看:140
本文介绍了单击联系人按钮而不是设备的默认视图时,在自定义表viewcontoller中显示iOS设备中的所有联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,当我单击联系人按钮时,我需要在自定义表viewcontoller中显示设备中的所有联系人。我发现了很多使用地址簿框架的演示,但是在其中使用ABPeoplePickerNavigationController以默认设备格式显示联系人。但我需要在自定义视图中显示所有联系人。有没有办法做到这一点。我的应用程序也应该在iOS7及更高版本中运行。可以在iOS 7和8中使用Contacts Framework吗?我正在使用xcode 7.2。请提供目标C中的解决方案,因为我不熟悉swift.Thanks提前。

In my app I need to display all contacts from the device in a custom table viewcontoller when I click contacts button. I found a lot of demos using addressbook framework but in it the contacts are displayed in default device format using ABPeoplePickerNavigationController. But I need to display all contacts in custom view. Is there any way to do that. Also my app should work in iOS7 and above. Can Contacts Framework be used in iOS 7 and 8? I'm using xcode 7.2. Please provide the solutions in objective C as I'm not familier with swift.Thanks in advance.

推荐答案

在iOS 9地址簿框架中不推荐使用。请使用Contact框架。

In iOS 9 AddressBook Framework is deprecated.Kindly use Contact framework.

我试过了,它的工作非常棒。

I tried, and it works fabulously.

非常完美的答案是下面。

Very Perfect answer is below.

#import "ViewController.h"
#import <Contacts/Contacts.h>
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
   UITableView *tableViewContactData;
   NSMutableArray *arrayTableData;
}

@end

@implementation ViewController

- (void)viewDidLoad 
{
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.

  arrayTableData = [[NSMutableArray alloc]init];
  [self fetchContactsandAuthorization];
  tableViewContactData = [[UITableView alloc] init];
  tableViewContactData.frame = CGRectMake(0, 50, 320, 518);
  tableViewContactData.delegate = self;
  tableViewContactData.dataSource = self;
  [tableViewContactData registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
  [self.view addSubview:tableViewContactData];
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

//UITAbleView Deleate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return  arrayTableData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static  NSString *strCell = @"cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
  if(cell==nil)
  {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
  }
  cell.textLabel.text = arrayTableData[indexPath.row];
  return cell;
}

//This is for fetching contacts from iPhone.Also It asks authorization permission.
-(void)fetchContactsandAuthorization
{
   // Request authorization to Contacts
   CNContactStore *store = [[CNContactStore alloc] init];
   [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
   if (granted == YES)
   {
      //keys with fetching properties
      NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
      NSString *containerId = store.defaultContainerIdentifier;
      NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
      NSError *error;
      NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
      if (error) 
      {
         NSLog(@"error fetching contacts %@", error);
      } 
      else 
      {
            NSString *phone;
            NSString *fullName;
            NSString *firstName;
            NSString *lastName;
            UIImage *profileImage;
            NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init];
            for (CNContact *contact in cnContacts) {
                // copy data to my custom Contacts class.
                firstName = contact.givenName;
                lastName = contact.familyName;
                if (lastName == nil) {
                    fullName=[NSString stringWithFormat:@"%@",firstName];
                }else if (firstName == nil){
                    fullName=[NSString stringWithFormat:@"%@",lastName];
                }
                else{
                    fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                }
                UIImage *image = [UIImage imageWithData:contact.imageData];
                if (image != nil) {
                    profileImage = image;
                }else{
                    profileImage = [UIImage imageNamed:@"person-icon.png"];
                }
                for (CNLabeledValue *label in contact.phoneNumbers) {
                    phone = [label.value stringValue];
                    if ([phone length] > 0) {
                        [contactNumbersArray addObject:phone];
                    }
                }
                NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
                [arrayTableData addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"fullName"]]];
                NSLog(@"The contactsArray are - %@",arrayTableData);
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [tableViewContactData reloadData];
            });
        }
    }
}];
}
@end

输出

The contactsArray are - (
"John Appleseed",
"Kate Bell",
"Anna Haro",
"Daniel Higgins",
"David Taylor",
"Hank Zakroff"
}

这篇关于单击联系人按钮而不是设备的默认视图时,在自定义表viewcontoller中显示iOS设备中的所有联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆