2013-08-01 4 views
0

iPad에서 연락처 세부 정보를 얻고 내 앱에서 사용하려면 어떻게해야합니까? 다음 코드를 사용하고 시뮬레이터에서 세부 정보를 가져 오는 중입니다. 하지만 ipad에서 실행하는 동안 연락처 이미지, 이메일 등 내가 정확하게 전화 번호를 받고있어지고 있어요. 아이 패드ipad에서 objective-c를 사용하여 연락처 세부 정보

답변

0

에서 실행하는 동안

ABAddressBookRef addressBook = ABAddressBookCreate(); 

// Get all contacts in the addressbook 
NSArray *allPeople = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 

for (id person in allPeople) { 
    // Get all phone numbers of a contact 
    ABMultiValueRef phoneNumbers = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty); 
    ABMultiValueRef emailaddress = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonEmailProperty); 


    // If the contact has multiple phone numbers, iterate on each of them 
    for (int i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) { 
     NSString *phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i); 



     // Remove all formatting symbols that might be in both phone number being compared 
     NSCharacterSet *toExclude = [NSCharacterSet characterSetWithCharactersInString:@"/.()- +"]; 
     phone = [[phone componentsSeparatedByCharactersInSet:toExclude] componentsJoinedByString: @""]; 


     if ([phone isEqualToString:number]) { 

      NSData *contactImageData = (__bridge NSData*)ABPersonCopyImageData((__bridge ABRecordRef)(person)); 
      NSString *mail = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(emailaddress, i); 
      NSLog(@"%@",mail); 
      if(mail) 
      { 
       hasEmail=TRUE; 
       NSLog(@"true"); 
      } 
      else{ 
       hasEmail=FALSE; 
       NSLog(@"false"); 
      } 

      ContactImage = [[UIImage alloc] initWithData:contactImageData]; 
      // [conImage setImage:ContactImage]; 

      break; 
      break; 
     } 
    } 
} 
if(ContactImage) 
{ 
    [conImage setImage:ContactImage]; 
} 
else{ 

    NSLog(@"no image"); 
} 

나는 당신은 ViewDidLoad 방법이 코드를 구현할 수있는 이미지를 얻을 필요가있다. 연락처를 얻기 위해

ABAddressBookRef addressBook; 
if ([self isABAddressBookCreateWithOptionsAvailable]) { 
    CFErrorRef error = nil; 
    addressBook = ABAddressBookCreateWithOptions(NULL,&error); 
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { 
     // callback can occur in background, address book must be accessed on thread it was created on 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (error) { 

      } else if (!granted) { 


      } else { 
       // access granted 
       [self GetAddressBook]; 


      } 
     }); 
    }); 

} else { 
    // iOS 4/5 

    [self GetAddressBook]; 
} 

방법 : -

-(void)GetAddressBook 
{ 
    Contacts = [[NSMutableArray alloc]init]; 

    if (ABAddressBookCreateWithOptions) { 

    @try { 

     ABAddressBookRef addressBook = ABAddressBookCreate(); 
     // NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 
     if (!addressBook) { 
      NSLog(@"opening address book"); 
     } 
     CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
     CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 

     NSLog(@"opening address book ==%ld",nPeople); 

     for (int i=0;i < nPeople;i++) { 

      NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary]; 
      ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i); 
      NSString *Contact; 
      ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty)); 
      CFStringRef firstName, lastName; 
      NSMutableArray *array = [[NSMutableArray alloc]init]; 
      NSString *email; 
      firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty); 
      lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty); 
      ABMultiValueRef multiValueRef = ABRecordCopyValue(ref, kABPersonEmailProperty); 
      array = [(__bridge NSMutableArray *)ABMultiValueCopyArrayOfAllValues(multiValueRef) mutableCopy]; 
      email = ([array count] > 0) ? array[0] : @""; 

      if(firstName) 
      { 
       Contact = [NSString stringWithFormat:@"%@", firstName]; 
       if(lastName) 
        Contact = [NSString stringWithFormat:@"%@ %@",firstName,lastName]; 
      } 
      [dOfPerson setObject:Contact forKey:@"name"]; 
      [dOfPerson setObject:[NSString stringWithFormat:@"%d", i] forKey:@"id"]; 
      [dOfPerson setObject:[NSString stringWithFormat:@"%@",@""] forKey:@"found"]; 
      [dOfPerson setObject:email forKey:@"email"]; 

      NSString* mobileLabel; 
      for(CFIndex j = 0; j< ABMultiValueGetCount(phones); j++) 
      { 
       mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, j); 
       if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) 
       { 
        [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"]; 
       } 
       else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) 
       { 
        [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"]; 
        break ; 
       } 
      } 
      [Contacts addObject:dOfPerson]; 
     } 
    } 
    @catch (NSException * e) { 
     NSLog(@"Exception: %@", e);   
    } 
    dispatch_async(dispatch_get_main_queue(), ^{   

    }); 

    NSLog(@"%@",Contacts); 
    if([Contacts count]>0) 
    { 
     NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; 
     [Contacts sortUsingDescriptors:[NSArray arrayWithObject:sort]]; 
     [Tableview reloadData]; 
    } 
    else 
    { 

    } 
} 
else 
{ 
    [self GetAddressBook]; 
} 
} 

여기 연락처NSMutableArray이며, 모든 데이터는 UITableView에 표시되고있다. 이 데이터에는 이름, 성, 전화 번호 및 전자 메일 주소가 포함됩니다. 희망이 도움이됩니다.

+0

제 코딩이 잘 작동합니다. 그러나 ipad를 사용하는 동안 나는 유일한 문제인 이미지와 이메일 ID를 얻지 못한다. – priya

+0

이 u에서 이메일 ID를 얻을 수있다 ..... – IronManGill

+0

하지만 이미지? 나는 이미지가 필요해. – priya

관련 문제