2016-08-14 96 views
-1

如何从应用程序中的电话簿中导入所有联系人。并且我们可以获得导入的联系人。如何将所有手机联系人添加到桌面查看

+2

安德烈看到这个完美的答案http://stackoverflow.com/questions/38261248/how-to-impliment-search-bar-in-table-view-for-contacts-in-ios/38264318#38264318 – user3182143

+1

也看到这个答案http://stackoverflow.com/questions/35595631/display-all-contacts-from-the-ios-device-in-custom-table-viewcontoller-when-cont/35603499#35603499 – user3182143

+1

我已经给尝试并在那里工作的解决方案。两者都将完美工作。 – user3182143

回答

0

安德烈兄弟如果你还没有得到答案,我给你答案。我试过并得到了解决方案。它工作正常。

AddressBookUI框架在iOS 9中不推荐使用,所以我们需要使用Contact Framework。

首先我使用XIB或Storyboard设置或连接tableView来显示联系人。

必须导入联系人框架

ViewController.h

#import <UIKit/UIKit.h> 
#import <Contacts/Contacts.h> 

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 
{ 
} 
@property (strong, nonatomic) IBOutlet UITableView *tableViewShowContacts; 
@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 
{ 
    NSMutableArray *arrayContacts; 
} 
@end 
@implementation ViewController 
@synthesize tableViewShowContacts; 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    arrayContacts = [[NSMutableArray alloc]init]; 
    [self getAuthorizationandContact]; 
    //Register the cell 
    [tableViewContactData registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; 
    [self.view addSubview:tableViewShowContacts]; 
} 

-(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]; 
      [arrayContacts addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"fullName"]]]; 
      NSLog(@"The contacts are - %@",arrayContacts); 
     } 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [tableViewShowContacts reloadData]; 
     }); 
     } 
    } 
}]; 
} 

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


#pragma mark - UITableView Data Source Methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return arrayContacts.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 = arrayContacts[indexPath.row]; 
    return cell; 
} 

联系的打印结果

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