2016-08-14 2 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またはストーリーボードを使用して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" 
) 
関連する問題