본문 바로가기

개발도구/iOS - 아이폰 개발

[Ios] AddressBook -주소록 테이블뷰 만들기

내 아이폰의 주소를 갖고 오기 위해서는 필요한 프레임워크가 있다.

3개의 프레임워크를 추가해준다.

기본적인 소스는

   ABAddressBookRef addressBook = ABAddressBookCreate();

//성과 이름 가지고오는

        NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

        NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

        

        //전화번호 가지고 온다

        ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);

        NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(multi);



스토리보드 테이블 과 연동하여 처리하여 한다.


소스는 이렇다.

.h

ABAddressBookRef _addressBook; // 어드레스북을 신디사이져처리위해
@property (nonatomic, retain) NSArray *contacts; // 주소를 담아 테이블 뷰에 넣기위한 작업


.m

- (void)viewDidLoad

{

    [super viewDidLoad];

 //주소록에 있는 것을 가져오는 API

NSMutableArray *adressBooks = [[NSMutableArray alloc] init];    ABAddressBookRef addressBook = ABAddressBookCreate();    NSMutableArray *allPeople = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); //어드레스 갯수를 구해 for문 과 사람을 배열로 나눌때 사용

   

//주소록을 정렬

[allPeople sortUsingFunction:(NSInteger (*)(id, id, void *))ABPersonComparePeopleByName context:(void *)ABPersonGetSortOrdering()]; 

for (int i = 0; i < [allPeople count]; i++) {
ABRecordRef person = [allPeople objectAtIndex:i];
 
  NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); //퍼스트네임이름갖고 오기
   NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);//라스트네임갖고오

 //전화번호 가지고 온다
 ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
  NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(multi);
       

  //이름, 성, 그리고 성과 이름을 통일하기위한 작업
   for(NSInteger i =0 ; i < [phoneNumbers count];i++) {
    NSString *num = [phoneNumbers objectAtIndex:i];
    }

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];      
    for (NSString *number in phoneNumbers) {
               NSString *name;
                if (lastName && firstName)
                    name = [[NSString alloc] initWithFormat:@"%@%@",lastName, firstName];
                else if (lastName)
                    name = [[NSString alloc] initWithFormat:@"%@",lastName];
                else if (firstName)
                    name = [[NSString alloc] initWithFormat:@"%@",firstName];                           

                   NSString *num = [[NSString alloc] initWithString:number];

                [dict setObject:[NSString stringWithFormat:@"%@", name] forKey:@"address_name"];

                [dict setObject:[NSString stringWithFormat:@"%@", num] forKey:@"address_num"];

            [adressBooks addObject:dict];

               if(name){
                    NSLog(@"name = %@",name);
                    NSLog(@"number = %@",num);
                }

        }

        self.contacts = adressBooks;

]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    [self.view setUserInteractionEnabled:YES];

    static NSString *CellIdentifier = @"AddressCell";
    AddressCell *cell = (AddressCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSObject *object = [self.contacts objectAtIndex:indexPath.row];

    if (cell == nil) {
cell = [[AddressCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]
    }

[cell
setSelectionStyle:UITableViewCellSelectionStyleNone];

  cell.name.text = [(NSDictionary *)object objectForKey:@"address_name"];

 cell.phonenumber.text = [(NSDictionary *)object objectForKey:@"address_num"];

}


참고블로그 : http://blog.naver.com/tanocrow87?Redirect=Log&logNo=150142013085