2011-07-23 126 views
1

有人可以告訴我如何從iPhone聯繫人列表中檢索姓氏和姓名,並將其顯示在個人應用程序中嗎?在iPhone聯繫人列表中,有些人有100個聯繫人,我想從這100個聯繫人中檢索名字和姓氏,並將它們顯示在我的應用程序中。從iPhone回顧聯繫人列表並在presonal應用程序中顯示

有沒有人有任何想法如何從聯繫人列表中檢索並顯示在我的應用程序中?

回答

3

您可以使用此方法將iPhone設備聯繫人信息同步到您自己的應用程序中,並可以顯示在您自己的列表中。

在這裏,您可以使用一個數組 - 「contactArray」來存儲聯繫人信息。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    contactArray = [[NSMutableArray alloc] init]; 

    [self SyncContactData]; 

    [self.window addSubview:navigationController.view]; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

這是爲了從設備到應用程序了同步聯繫人信息的方法。

- (void) SyncContactData 
{ 
    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 

    for(int i = 0 ; i < nPeople ; i++) 
    { 
     dicContact = [[NSMutableDictionary alloc] init]; 

     ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i); 

     if(ABRecordCopyValue(ref, kABPersonFirstNameProperty) != nil || [[NSString stringWithFormat:@"%@",ABRecordCopyValue(ref, kABPersonFirstNameProperty)] length] == 0) 
      [dicContact setValue:[NSString stringWithFormat:@"%@",ABRecordCopyValue(ref, kABPersonFirstNameProperty)] forKey:@"firstname"]; 
     else 
      [dicContact setValue:@"" forKey:@"firstname"]; 

     if(ABRecordCopyValue(ref, kABPersonLastNameProperty) != nil || [[NSString stringWithFormat:@"%@",ABRecordCopyValue(ref, kABPersonLastNameProperty)] length] == 0) 
      [dicContact setValue:[NSString stringWithFormat:@"%@",ABRecordCopyValue(ref, kABPersonLastNameProperty)] forKey:@"lastname"]; 
     else 
      [dicContact setValue:@"" forKey:@"lastname"]; 

     if(ABRecordCopyValue(ref, kABPersonOrganizationProperty) != nil || [[NSString stringWithFormat:@"%@",ABRecordCopyValue(ref, kABPersonOrganizationProperty)] length] == 0) 
      [dicContact setValue:[NSString stringWithFormat:@"%@",ABRecordCopyValue(ref, kABPersonOrganizationProperty)] forKey:@"name"]; 
     else 
      [dicContact setValue:[NSString stringWithFormat:@"%@ %@",[dicContact valueForKey:@"firstname"],[dicContact valueForKey:@"lastname"]] forKey:@"name"]; 

     NSData *data1 = [(NSData *) ABPersonCopyImageData(ref) autorelease]; 

     if(data1 == nil) 
      [dicContact setObject:@"" forKey:@"image"]; 
     else 
      [dicContact setObject:data1 forKey:@"image"]; 

     multival = ABRecordCopyValue(ref, kABPersonAddressProperty); 
     NSArray *arrayAddress = (NSArray *)ABMultiValueCopyArrayOfAllValues(multival); 
     if([arrayAddress count] > 0) 
     { 
      if([[arrayAddress objectAtIndex:0] valueForKey:@"City"] != nil) 
       [dicContact setValue:[[arrayAddress objectAtIndex:0] valueForKey:@"City"] forKey:@"city"]; 
      else 
       [dicContact setValue:@"" forKey:@"city"]; 

      if([[arrayAddress objectAtIndex:0] valueForKey:@"State"] != nil) 
       [dicContact setValue:[[arrayAddress objectAtIndex:0] valueForKey:@"State"] forKey:@"state"]; 
      else 
       [dicContact setValue:@"" forKey:@"state"]; 

      if([[arrayAddress objectAtIndex:0] valueForKey:@"Street"] != nil) 
       [dicContact setValue:[[arrayAddress objectAtIndex:0] valueForKey:@"Street"] forKey:@"address1"]; 
      else 
       [dicContact setValue:@"" forKey:@"address1"]; 

      if([[arrayAddress objectAtIndex:0] valueForKey:@"ZIP"] != nil) 
       [dicContact setValue:[[arrayAddress objectAtIndex:0] valueForKey:@"ZIP"] forKey:@"postcode"]; 
      else 
       [dicContact setValue:@"" forKey:@"postcode"]; 
     } 
     else 
     { 
      [dicContact setValue:@"" forKey:@"city"]; 
      [dicContact setValue:@"" forKey:@"address1"]; 
      [dicContact setValue:@"" forKey:@"state"]; 
      [dicContact setValue:@"" forKey:@"postcode"]; 
     } 

     multival = ABRecordCopyValue(ref, kABPersonPhoneProperty); 
     NSArray *arrayPhone = (NSArray *)ABMultiValueCopyArrayOfAllValues(multival); 
     if([arrayPhone count] > 0) 
      [dicContact setValue:[arrayPhone objectAtIndex:0] forKey:@"telephone"]; 
     else 
      [dicContact setValue:@"" forKey:@"telephone"]; 

     multival = ABRecordCopyValue(ref, kABPersonEmailProperty); 
     NSArray *arrayEmail = (NSArray *)ABMultiValueCopyArrayOfAllValues(multival); 
     if([arrayEmail count]) 
      [dicContact setValue:[arrayEmail objectAtIndex:0] forKey:@"email"]; 
     else 
      [dicContact setValue:@"" forKey:@"email"]; 

     multival = ABRecordCopyValue(ref, kABPersonURLProperty); 
     NSArray *arrayURL = (NSArray *)ABMultiValueCopyArrayOfAllValues(multival); 
     if([arrayURL count]) 
      [dicContact setValue:[arrayURL objectAtIndex:0] forKey:@"website"]; 
     else 
      [dicContact setValue:@"" forKey:@"website"]; 

     [dicContact setValue:@"" forKey:@"address2"]; 
     [dicContact setValue:@"" forKey:@"mobile"]; 
     [dicContact setValue:@"" forKey:@"fax"]; 
     [dicContact setValue:@"1.000000,1.000000,0.000000,0.000000" forKey:@"color"]; 

     [contactArray addObject:dicContact]; 
     [dicContact release]; 
    } 

    CFRelease(addressBook); 
    CFRelease(allPeople); 
} 
+0

這是與我的代碼? – AppAspect

相關問題