我是iOS应用开发的新手,希望在目标c中获取iOS设备的所有联系方式。
请帮帮我。
答案 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
}
}];
以下链接可以帮助您更好。