不,您必须对所有属性执行此操作,除此之外别无他法。然而,这里是所有这些获取细节的代码。这里可能有一些额外的东西,因为我是为我的应用程序做的。不一般。这里的联系人是我自己的对象,其中包含名称、公司和包含属性的属性数组等数据。这不是完美的代码,只是为了展示 ua 模型,分为两部分:
-(NSMutableArray*)getPhoneContacts
{
NSLog(@"getPhoneContacts");
NSAutoreleasePool *pool= [[NSAutoreleasePool alloc] init];
NSMutableArray *tempArray=[[NSMutableArray alloc] init];
ABAddressBookRef iPhoneAddressBook=ABAddressBookCreate();
if(!iPhoneAddressBook)
{
DLog(@"unable to open addressBook");
}
for(i=0;i<[peopleArray count];i++)
{
if((i&255)==0)
{
innerPool= [[NSAutoreleasePool alloc] init];
shouldReleasePool=YES;
}
ABRecordRef record=[peopleArray objectAtIndex:i];
Contact *objPhoneContact=[[Contact alloc] init];
objPhoneContact.contactType=STATIC_CONTACT;
int32_t uniqueId=ABRecordGetRecordID(record);
objPhoneContact.addressBookId=uniqueId;
CFStringRef temp;
temp=ABRecordCopyValue(record, kABPersonPrefixProperty);
if (temp) {
objPhoneContact.mPrefixName=[NSString stringWithString:(NSString*)temp];
CFRelease(temp);
}
temp=ABRecordCopyValue(record, kABPersonMiddleNameProperty);
if (temp) {
objPhoneContact.mMiddleName=[NSString stringWithString:(NSString*)temp];
CFRelease(temp);
}
temp=ABRecordCopyValue(record, kABPersonNicknameProperty);
if (temp) {
objPhoneContact.mNickName=[NSString stringWithString:(NSString*)temp];
CFRelease(temp);
}
temp=ABRecordCopyValue(record, kABPersonSuffixProperty);
if (temp) {
objPhoneContact.mSuffixName=[NSString stringWithString:(NSString*)temp];
CFRelease(temp);
}
ContactProperty* objAddressContactProperty;
int propertyIndex=0;
temp=ABRecordCopyValue(record, kABPersonFirstNamePhoneticProperty);
if (temp) {
propertyIndex=4;
objAddressContactProperty=[[ContactProperty alloc] init];
objAddressContactProperty.mDisplayName=@"PhoneticFirstName";
objAddressContactProperty.mContactPropId=propertyIndex;
objAddressContactProperty.mContactDataType=@"Text";
objAddressContactProperty.mContactPropertyString=[NSString stringWithString:(NSString*)temp];
[objPhoneContact.mPropertyArray addObject:objAddressContactProperty];
[objAddressContactProperty release];
CFRelease(temp);
}
temp=ABRecordCopyValue(record, kABPersonLastNamePhoneticProperty);
if (temp) {
propertyIndex=5;
objAddressContactProperty=[[ContactProperty alloc] init];
objAddressContactProperty.mDisplayName=@"PhoneticLastName";
objAddressContactProperty.mContactPropId=propertyIndex;
objAddressContactProperty.mContactDataType=@"Text";
objAddressContactProperty.mContactPropertyString=[NSString stringWithString:(NSString*)temp];
[objPhoneContact.mPropertyArray addObject:objAddressContactProperty];
[objAddressContactProperty release];
CFRelease(temp);
}
temp=ABRecordCopyValue(record, kABPersonJobTitleProperty);
if (temp) {
propertyIndex=6;
objAddressContactProperty=[[ContactProperty alloc] init];
objAddressContactProperty.mDisplayName=@"JobTitle";
objAddressContactProperty.mContactPropId=propertyIndex;
objAddressContactProperty.mContactDataType=@"Text";
objAddressContactProperty.mContactPropertyString=[NSString stringWithString:(NSString*)temp];
[objPhoneContact.mPropertyArray addObject:objAddressContactProperty];
[objAddressContactProperty release];
CFRelease(temp);
}
temp=ABRecordCopyValue(record, kABPersonDepartmentProperty);
if (temp) {
propertyIndex=7;
objAddressContactProperty=[[ContactProperty alloc] init];
objAddressContactProperty.mDisplayName=@"Department";
objAddressContactProperty.mContactPropId=propertyIndex;
objAddressContactProperty.mContactDataType=@"Text";
objAddressContactProperty.mContactPropertyString=[NSString stringWithString:(NSString*)temp];
[objPhoneContact.mPropertyArray addObject:objAddressContactProperty];
[objAddressContactProperty release];
CFRelease(temp);
}
NSDate *tempDate=nil;
tempDate=(NSDate*)ABRecordCopyValue(record, kABPersonBirthdayProperty);
if (tempDate) {
propertyIndex=1401;
objAddressContactProperty=[[ContactProperty alloc] init];
objAddressContactProperty.mDisplayName=@"BirthDay";
objAddressContactProperty.mContactPropId=propertyIndex;
objAddressContactProperty.mContactDataType=@"Date";
objAddressContactProperty.mContactPropertyString=[NSString stringWithFormat:@"%@",tempDate];
[objPhoneContact.mPropertyArray addObject:objAddressContactProperty];
[objAddressContactProperty release];
[tempDate release];
// CFRelease(temp);
}
temp=ABRecordCopyValue(record, kABPersonNoteProperty);
if (temp) {
propertyIndex=1901;
objAddressContactProperty=[[ContactProperty alloc] init];
objAddressContactProperty.mDisplayName=@"Note";
objAddressContactProperty.mContactPropId=propertyIndex;
objAddressContactProperty.mContactDataType=@"Text";
objAddressContactProperty.mContactPropertyString=[NSString stringWithString:(NSString*)temp];
[objPhoneContact.mPropertyArray addObject:objAddressContactProperty];
[objAddressContactProperty release];
CFRelease(temp);
}
NSDate *localSyncDate=[[DataModel sharedDataModel] getValueForKey:LOCAL_SYNC_TIME_FOR_MODIFICATION];
// Check modified Date
CFDateRef modDate = ABRecordCopyValue(record, kABPersonModificationDateProperty);
//NSDate *lastSyncTime = [[NSDate alloc] initWithString:lastSyncTimeStr];
DLog(@"Record's modification date is: %@",(NSDate*)modDate);
// NSLog(@"%@ and %@",modDate,localSyncDate);
NSComparisonResult result = [localSyncDate compare:(NSDate*)modDate];
if (result==-1) {
if (appDelegate.shouldUpdateContactsCounter && appDelegate.isSyncInProgress!=0) {
appDelegate.updatedInDeviceCounter++;
}
DLog(@"lastSyncTime is: %@",localSyncDate);
DLog(@"Record's modification date is: %@",(NSDate*)modDate);
}
CFRelease(modDate);
DLog(@"Date Comparison Result is: %d",result);
objPhoneContact.mDateComparisionResult = result;
/*switch (result)
{
case NSOrderedAscending:
DLog(@"moddate=%@ is in future from lastsynctime=%@", modDate, lastSyncTime);
// the contact has been modified/updated
break;
case NSOrderedDescending:
DLog(@"moddate=%@ is in past from lastsynctime=%@", modDate, lastSyncTime);
// No need to update the contact
CFRelease(record);
[objPhoneContact release];
continue;
case NSOrderedSame: DLog(@"moddate=%@ is the same as lastsynctime=%@", modDate, lastSyncTime); break;
default: DLog(@"erorr dates %@, %@", modDate, lastSyncTime); break;
}*/
// Get NoteToSelf
/*kABPersonNoteProperty*/
temp=ABRecordCopyValue(record, kABPersonOrganizationProperty);
if (temp!=nil)
{
NSUInteger OrganizationPropertyId=3;
ContactProperty* objCompanyContactProperty=[[ContactProperty alloc] init];
NSMutableString *companyname=[NSMutableString stringWithFormat:@"%@",(NSString*)temp];
objCompanyContactProperty.mContactPropertyString=[companyname stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];;
objCompanyContactProperty.mDisplayName=@"Company";
objCompanyContactProperty.mContactPropId=OrganizationPropertyId;
objCompanyContactProperty.mContactDataType=@"Text";
[objPhoneContact.mPropertyArray addObject:objCompanyContactProperty];
[objCompanyContactProperty release];
CFRelease(temp);
// temp=nil;
}
if (ABPersonHasImageData(record))
{
CFDataRef* dataRef=ABPersonCopyImageData(record);
NSData* imageData=[[NSData alloc] initWithData:(NSData*)dataRef];;
UIImage *img = [ImageUtility resizedImageWithImage:[UIImage imageWithData:imageData] scaledToDimension:CGSizeMake(50, 50)]; // Scale it to 50x50px
objPhoneContact.mUserPhotoData = [NSData dataWithData:UIImageJPEGRepresentation(img, 1.0f)];
[imageData release];
if(dataRef)
CFRelease(dataRef);
}
ABMutableMultiValueRef multi;
int multiCount=0;
multi = ABRecordCopyValue(record, kABPersonDateProperty);
multiCount=ABMultiValueGetCount(multi);
if(multiCount ==0)
{
}
else
{
int propertyIndex=1402;
for (CFIndex i = 0; i < multiCount; i++)
{
ContactProperty* objPhoneContactProperty=[[ContactProperty alloc] init];
objPhoneContactProperty.mContactPropertyString=[NSString stringWithFormat:@"%@",(NSDate*)ABMultiValueCopyValueAtIndex(multi, i)];
objPhoneContactProperty.mDisplayName=(NSString*)ABMultiValueCopyLabelAtIndex(multi, i);
objPhoneContactProperty.mDisplayName=[objPhoneContactProperty.mDisplayName stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"_$!<>"]];
objPhoneContactProperty.mContactPropId=propertyIndex;
objPhoneContactProperty.mContactDataType=@"Date";
[objPhoneContact.mPropertyArray addObject:objPhoneContactProperty];
[objPhoneContactProperty release];
propertyIndex++;
}
}
if(multi)
CFRelease(multi);
multi = ABRecordCopyValue(record, kABPersonPhoneProperty);
NSUInteger phoneIndex_mobile=11;
NSUInteger phoneIndex_iPhone=31;
NSUInteger phoneIndex_home=51;
NSUInteger phoneIndex_work=71;
NSUInteger phoneIndex_main=91;
NSUInteger phoneIndex_home_fax=111;
NSUInteger phoneIndex_work_fax=131;
NSUInteger phoneIndex_pager=151;
NSUInteger phoneIndex_other=171;
multiCount=ABMultiValueGetCount(multi);
if(multiCount ==0)
{
//objPhoneContact.mPhoneNum=@"";
}
else
{
for( int i=0; i < multiCount; i++)
{
ContactProperty* objPhoneContactProperty=[[ContactProperty alloc] init];
objPhoneContactProperty.mContactPropertyString=(NSString*)ABMultiValueCopyValueAtIndex(multi, i);
objPhoneContactProperty.mDisplayName=(NSString*)ABMultiValueCopyLabelAtIndex(multi, i);
objPhoneContactProperty.mDisplayName=[objPhoneContactProperty.mDisplayName stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"_$!<>"]];
if([objPhoneContactProperty.mDisplayName caseInsensitiveCompare:@"mobile"]==NSOrderedSame)
{
objPhoneContactProperty.mContactPropId=phoneIndex_mobile;
phoneIndex_mobile++;
}
else if([objPhoneContactProperty.mDisplayName caseInsensitiveCompare:@"iPhone"]==NSOrderedSame)
{
objPhoneContactProperty.mContactPropId=phoneIndex_iPhone;
phoneIndex_iPhone++;
}
else if([objPhoneContactProperty.mDisplayName caseInsensitiveCompare:@"home"]==NSOrderedSame)
{
objPhoneContactProperty.mContactPropId=phoneIndex_home;
phoneIndex_home++;
}
else if([objPhoneContactProperty.mDisplayName caseInsensitiveCompare:@"work"]==NSOrderedSame)
{
objPhoneContactProperty.mContactPropId=phoneIndex_work;
phoneIndex_work++;
}
else if([objPhoneContactProperty.mDisplayName caseInsensitiveCompare:@"main"]==NSOrderedSame)
{
objPhoneContactProperty.mContactPropId=phoneIndex_main;
phoneIndex_main++;
}
else if([objPhoneContactProperty.mDisplayName caseInsensitiveCompare:@"HomeFAX"]==NSOrderedSame)
{
objPhoneContactProperty.mContactPropId=phoneIndex_home_fax;
phoneIndex_home_fax++;
}
else if([objPhoneContactProperty.mDisplayName caseInsensitiveCompare:@"WorkFAX"]==NSOrderedSame)
{
objPhoneContactProperty.mContactPropId=phoneIndex_work_fax;
phoneIndex_work_fax++;
}
else if([objPhoneContactProperty.mDisplayName caseInsensitiveCompare:@"pager"]==NSOrderedSame)
{
objPhoneContactProperty.mContactPropId=phoneIndex_pager;
phoneIndex_pager++;
}
else
{
NSRange range1,range2,range3;
range1=[objPhoneContactProperty.mDisplayName rangeOfString:@"mobile" options:NSCaseInsensitiveSearch];
range2=[objPhoneContactProperty.mDisplayName rangeOfString:@"home" options:NSCaseInsensitiveSearch];
range3=[objPhoneContactProperty.mDisplayName rangeOfString:@"work" options:NSCaseInsensitiveSearch];
if (range1.location!=NSNotFound)
{
objPhoneContactProperty.mContactPropId=phoneIndex_mobile;
phoneIndex_mobile++;
}
else if (range2.location!=NSNotFound)
{
objPhoneContactProperty.mContactPropId=phoneIndex_home;
phoneIndex_home++;
}
else if (range3.location!=NSNotFound)
{
objPhoneContactProperty.mContactPropId=phoneIndex_work;
phoneIndex_work++;
}
else
{
objPhoneContactProperty.mContactPropId=phoneIndex_other;
phoneIndex_other++;
}
}
objPhoneContactProperty.mContactDataType=@"Phone";
[objPhoneContact.mPropertyArray addObject:objPhoneContactProperty];
[objPhoneContactProperty release];
}
}
if(multi)
CFRelease(multi);
multi=ABRecordCopyValue(record,kABPersonEmailProperty);
NSUInteger emailIndex_home=301;
NSUInteger emailIndex_work=321;
NSUInteger emailIndex_other=341; //~400
multiCount=ABMultiValueGetCount(multi);
if(multiCount ==0)
{
//objPhoneContact.mEmailId=@"";
}
else
{
for( int i=0; i < multiCount; i++)
{
ContactProperty* objEmailContactProperty=[[ContactProperty alloc] init];
objEmailContactProperty.mContactPropertyString=(NSString*)ABMultiValueCopyValueAtIndex(multi, i);
objEmailContactProperty.mDisplayName=(NSString*)ABMultiValueCopyLabelAtIndex(multi, i);
objEmailContactProperty.mDisplayName=[objEmailContactProperty.mDisplayName stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"_$!<>"]];
if([objEmailContactProperty.mDisplayName caseInsensitiveCompare:@"home"]==NSOrderedSame)
{
objEmailContactProperty.mContactPropId=emailIndex_home;
emailIndex_home++;
}
else if([objEmailContactProperty.mDisplayName caseInsensitiveCompare:@"work"]==NSOrderedSame)
{
objEmailContactProperty.mContactPropId=emailIndex_work;
emailIndex_work++;
}
else
{
objEmailContactProperty.mContactPropId=emailIndex_other;
emailIndex_other++;
}
objEmailContactProperty.mContactDataType=@"Email";
[objPhoneContact.mPropertyArray addObject:objEmailContactProperty];
[objEmailContactProperty release];
}
}
if(multi)
CFRelease(multi);
NSUInteger imIndex_HomeUser=1601;
NSUInteger imIndex_HomeService=1602;
NSUInteger imIndex_WorkUser=1651;
NSUInteger imIndex_WorkService=1652;
NSUInteger imIndex_OtherUser=1701;
NSUInteger imIndex_OtherService=1702;
multi = ABRecordCopyValue(record, kABPersonInstantMessageProperty);
multiCount=ABMultiValueGetCount(multi);
if(multiCount ==0)
{
//objPhoneContact.mEmailId=@"";
}
else
{
for(int i=0;i<multiCount;i++)
{
int propertyIndexUser=0;
int propertyIndexService=0;