如何使用APAddressBook从iOS中的电话联系人列表中检索联系人详细信息?

时间:2016-02-17 13:35:39

标签: ios objective-c

我是iOS应用开发的新手,希望在目标c中获取iOS设备的所有联系方式。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

尝试下面的代码我希望这会对你有所帮助。 这段代码对我有用。

APAddressBook *addressBook = [[APAddressBook alloc] init];
[addressBook loadContacts:^(NSArray <APContact *> *contacts, NSError *error)
 {
     // hide activity
     if (!error)
     {
         for (APContact *strcontact in contacts)
         {
             NSMutableDictionary *addressDict=[[NSMutableDictionary alloc]init];
             [addressDict setValue:strcontact.name.compositeName forKey:COMPOSITENAME];

             if (strcontact.thumbnail != nil)
             {
                 NSData *imgData = [NSData dataWithData:UIImagePNGRepresentation(strcontact.thumbnail)];
                 // From data to string
                 NSString *strImage = [[NSString alloc] initWithData:imgData encoding:NSUTF8StringEncoding];
                 [addressDict setObject:strImage forKey:IMAGE];
             }
             else
             {
                 [addressDict setObject:NOIMAGE forKey:IMAGE];
             }

             for (APPhone *phones in strcontact.phones) {
                 //  NSLog(@"Number %@",phones.number);
                 [addressDict setValue:phones.number forKey:PHONE];
             }
             for (APEmail *emails in strcontact.emails) {
                 //   NSLog(@"Emails %@",emails.address);
                 [addressDict setValue:emails.address forKey:EMAIL];
             }
             for (APAddress *address in strcontact.addresses) {
                 //  NSLog(@"Number %@",phones.number);
                 [addressDict setValue:address forKey:ADDRESS];
             }

             NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
             formatter.dateFormat = @"dd.MM.yyyy";
             NSString *strDate = [formatter stringFromDate:[NSDate date]];
             [addressDict setValue:strDate forKey:IMPORTDATE];

             [arrContacts addObject:addressDict];
         }
         [tblmportContacts reloadData];
         [self contactSaveIntoDB];
     }
     else
     {
         // show error
     }
 }];

以下链接可以帮助您更好。

https://github.com/Alterplay/APAddressBook