2010-06-06 5 views
1

iPhone SDK에서 주소록 사용 경험이 거의 없습니다.iPhone 사용자 주소록에서 임의의 이름과 번호 (동일한 연락처) 가져 오기

사람이 코드 조각을하거나 사람의 이름과 전화 번호 예 '프레드 스미스 얻을 수있는 코드를 알고 있나요 -. 027 292 2112 "내가 해봤 물건과 많은 행운이 없었어요

. 나는 프로그래밍 방식을 달성, 그리고 사용자가 (사람의 임의 선택)을 결정하자. 나는 또한, 접촉 선택 도구 중 하나

어떤 튜토리얼 링크를 표시하지 않거나 아무것도 주시면 감사하겠습니다 싶습니다.

미리 감사드립니다.

Sam

답변

0

이 코드는 주소록에서 임의의 연락처를 가져옵니다. 그런 다음 UILabel 속성 (첫 번째, 마지막 전화 및 전화)에 이름과 성 및 첫 번째 전화 번호를 추가합니다.

먼저이 헤더 파일을 가져와야합니다,이 일을하려면

#import <AddressBook/AddressBook.h> 
#import <stdlib.h> 

stdlib.h이 arc4random을 사용할 수 있도록 가져옵니다.

- (IBAction)randomContact:(id)sender { 
    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    NSArray *people = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); 

    // randomize an index 
    NSInteger i = [people count]; 
    i = arc4random() % i; 

    // get first and last name 
    first.text = (NSString *)ABRecordCopyValue([people objectAtIndex:i], kABPersonFirstNameProperty); 
    last.text = (NSString *)ABRecordCopyValue([people objectAtIndex:i], kABPersonLastNameProperty); 


    // get first phonenumber 
    ABMultiValueRef multi = ABRecordCopyValue([people objectAtIndex:i], kABPersonPhoneProperty); 
    phone.text = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0); 


    [people release]; 
} 
관련 문제