我是 iphone 的新手。目前我正在从事项目工作,但由于不知道如何通过编码从 iphone 联系人列表中访问每个联系人的所有电子邮件 ID,因此我在项目中间遇到了麻烦。如果有人知道这一点概念你能帮我吗....
问问题
71 次
2 回答
0
此代码也为您提供电子邮件以及他们的显示名称和联系人姓名
-(NSMutableArray*)getEmailAndPhoneOfPhoneContacts {
NSLog(@"getPhoneContacts");
NSAutoreleasePool *pool= [[NSAutoreleasePool alloc] init];
NSMutableArray *tempArray=[[NSMutableArray alloc] init];
ABAddressBookRef iPhoneAddressBook=ABAddressBookCreate();
if(!iPhoneAddressBook)
{
DLog(@"unable to open addressBook");
}
//ABPerson *newPerson;
NSAutoreleasePool *innerPool=nil;
CFArrayRef allPeople=ABAddressBookCopyArrayOfAllPeople(iPhoneAddressBook);
NSMutableArray *peopleArray=[NSMutableArray arrayWithArray:(NSMutableArray*)allPeople];
BOOL shouldReleasePool=NO;
NSInteger i;
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;
CFStringRef prefixName=ABRecordCopyValue(record, kABPersonPrefixProperty);
CFStringRef firstName=ABRecordCopyValue(record, kABPersonFirstNameProperty);
CFStringRef middleName=ABRecordCopyValue(record, kABPersonMiddleNamePhoneticProperty);
CFStringRef lastName=ABRecordCopyValue(record, kABPersonLastNameProperty);
CFStringRef suffixName=ABRecordCopyValue(record, kABPersonSuffixProperty);
NSMutableString *contactname =[[NSMutableString alloc] init];
// concating all the names
if (prefixName) {
[contactname appendString:[NSString stringWithString:(NSString*)prefixName]];
CFRelease(prefixName);
}
if (firstName) {
[contactname appendString:[NSString stringWithString:(NSString*)firstName]];
CFRelease(firstName);
}
if (middleName) {
[contactname appendString:[NSString stringWithString:(NSString*)middleName]];
CFRelease(middleName);
}
if (lastName) {
[contactname appendString:[NSString stringWithString:(NSString*)lastName]];
CFRelease(lastName);
}
if (suffixName) {
[contactname appendString:[NSString stringWithString:(NSString*)suffixName]];
CFRelease(suffixName);
}
// if emty then get the organization property
if (contactname == nil || [contactname length]<1) {
CFStringRef orgName=ABRecordCopyValue(record, kABPersonOrganizationProperty);
if (orgName) {
[contactname appendString:[NSString stringWithString:(NSString*)orgName]];
CFRelease(orgName);
}
}
//if still empty then assign (no name) to it
if (contactname == nil || [contactname length]<1)
[contactname setString:@"(no name)"];
objPhoneContact.mContactName = [contactname stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[contactname release];
contactname = nil;
ABMutableMultiValueRef multi;
int multiCount=0;
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 if([objEmailContactProperty.mDisplayName caseInsensitiveCompare:@"other"]==NSOrderedSame)
{
objEmailContactProperty.mContactPropId=[NSString stringWithFormat:@"%d",emailIndex_other];
emailIndex_other++;
}
*/
else
{
objEmailContactProperty.mContactPropId=emailIndex_other;
emailIndex_other++;
}
objEmailContactProperty.mContactDataType=@"Email";
[objPhoneContact.mPropertyArray addObject:objEmailContactProperty];
[objEmailContactProperty release];
}
}
if(multi)
CFRelease(multi);
[tempArray addObject:objPhoneContact];
[objPhoneContact release];
if(shouldReleasePool)
{
[innerPool drain];
shouldReleasePool=NO;
}
}
self.mPhoneContactArray=tempArray;
CFRelease(iPhoneAddressBook);
CFRelease(allPeople);
[pool drain];
return [tempArray autorelease];
}
于 2012-05-09T09:03:20.130 回答
0
确保导入这两个文件:
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
-(IBAction)showPeoplePickerController
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
[menuArray removeAllObjects];
if(menuArray ==nil)
menuArray = [[NSMutableArray alloc] init];
for (int i = 0; i < nPeople; i++)
{
NSMutableDictionary *localDic=[[NSMutableDictionary alloc]init];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
ABRecordID recordId = ABRecordGetRecordID(ref);
[localDic setObject:[NSString stringWithFormat:@"%d",recordId] forKey:@"Record_Id"];
//get firstname
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
[localDic setObject:[NSString stringWithFormat:@"%@",firstName] forKey:@"first_name"];
//get lastname
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
[localDic setObject:[NSString stringWithFormat:@"%@",lastName] forKey:@"last_name"];
NSString *contactFirstLast = [NSString stringWithFormat: @"%@ %@",firstName,lastName];
[localDic setObject:contactFirstLast forKey:@"FullName"];
//get EmailIds
ABMutableMultiValueRef EmailIds = ABRecordCopyValue(ref, kABPersonEmailProperty);
CFTypeRef EmailId;
NSString *EmailLabel;
for (CFIndex i = 0; i < ABMultiValueGetCount(EmailIds); i++) {
EmailLabel=[NSString stringWithFormat:@"%@",ABMultiValueCopyLabelAtIndex(EmailIds,i)];
if([EmailLabel isEqualToString:@"_$!<Home>!$_"])
{
EmailId = ABMultiValueCopyValueAtIndex(EmailIds,i);
[localDic setObject:[NSString stringWithFormat:@"%@",EmailId] forKey:@"Email_Home"];
}
else if([EmailLabel isEqualToString:@"_$!<Work>!$_"])
{
EmailId = ABMultiValueCopyValueAtIndex(EmailIds,i);
[localDic setObject:[NSString stringWithFormat:@"%@",EmailId] forKey:@"Email_Work"];
break;
}
}
[menuArray addObject:localDic];
}
NSLog(@"%@",menuArray);
}
通过这种方式,您可以获得每个人的所有 EmailId。
并在您的项目中添加这两个框架:
AddressBook.framework
AddressBookUI.framework
于 2012-05-09T09:14:08.443 回答