繁体   English   中英

单击联系人按钮而不是设备的默认视图时,在自定义表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

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

在iOS 9中,不推荐使用AddressBook Framework。请使用Contact框架。

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

非常完美的答案如下。

#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"
}

简单使用地址簿框架,

只需点击链接即可

使用此链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM