如何获取iPhone的联系人详细信息并制作该联系人的CSV文件

Viv*_*012 8 csv iphone import-contacts

我想在iPhone上获取联系方式,包括姓名,姓氏,电话号码,电话号码类型,电子邮件地址,电子邮件地址类型等信息.

任何人都可以帮助我吗?

我想从特定iPhone中的联系人详细信息中创建一个.csv文件.我想获取iPhone通讯录数据.

all*_*Nit 13

以下是获取iPhone联系人书籍所有信息的代码...

    -(void)collectContacts
    {
        NSMutableDictionary *myAddressBook = [[NSMutableDictionary alloc] init];
        ABAddressBookRef addressBook = ABAddressBookCreate();
        CFArrayRef people  = ABAddressBookCopyArrayOfAllPeople(addressBook);
        for(int i = 0;i<ABAddressBookGetPersonCount(addressBook);i++)
        {
            ABRecordRef ref = CFArrayGetValueAtIndex(people, i);

            // Get First name, Last name, Prefix, Suffix, Job title 
            NSString *firstName = (NSString *)ABRecordCopyValue(ref,kABPersonFirstNameProperty);
            NSString *lastName = (NSString *)ABRecordCopyValue(ref,kABPersonLastNameProperty);
            NSString *prefix = (NSString *)ABRecordCopyValue(ref,kABPersonPrefixProperty);
            NSString *suffix = (NSString *)ABRecordCopyValue(ref,kABPersonSuffixProperty);
            NSString *jobTitle = (NSString *)ABRecordCopyValue(ref,kABPersonJobTitleProperty);

            [myAddressBook setObject:firstName forKey:@"firstName"];
            [myAddressBook setObject:lastName forKey:@"lastName"];
            [myAddressBook setObject:prefix forKey:@"prefix"];
            [myAddressBook setObject:suffix forKey:@"suffix"];
            [myAddressBook setObject:jobTitle forKey:@"jobTitle"];

            NSMutableArray *arPhone = [[NSMutableArray alloc] init];
            ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
            for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
            {       
                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);   
                NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel (ABMultiValueCopyLabelAtIndex(phones, j));
                NSString *phoneNumber = (NSString *)phoneNumberRef; 
                NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
                [temp setObject:phoneNumber forKey:@"phoneNumber"];
                [temp setObject:phoneLabel forKey:@"phoneNumber"];
                [arPhone addObject:temp];
                [temp release];
            }
            [myAddressBook setObject:arPhone forKey:@"Phone"];
            [arPhone release];            

            CFStringRef address;
            CFStringRef label;
            ABMutableMultiValueRef multi = ABRecordCopyValue(ref, kABPersonAddressProperty);    
            for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) 
            {           
                label = ABMultiValueCopyLabelAtIndex(multi, i);
                CFStringRef readableLabel = ABAddressBookCopyLocalizedLabel(label);             
                address = ABMultiValueCopyValueAtIndex(multi, i);   
                CFRelease(address);
                CFRelease(label);
            } 

            ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
            NSMutableArray *arEmail = [[NSMutableArray alloc] init];
            for(CFIndex idx = 0; idx < ABMultiValueGetCount(emails); idx++)
            {
                CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, idx);
                NSString *strLbl = (NSString*) ABAddressBookCopyLocalizedLabel (ABMultiValueCopyLabelAtIndex (emails, idx));
                NSString *strEmail_old = (NSString*)emailRef;
                NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
                [temp setObject:strEmail_old forKey:@"strEmail_old"];
                [temp setObject:strLbl forKey:@"strLbl"];
                [arEmail addObject:temp];
                [temp release];
            }
            [myAddressBook setObject:arEmail forKey:@"Email"];
            [arEmail release];
        }
        [self createCSV:myAddressBook];
    }

    -(void) createCSV :(NSMutableDictionary*)arAddressData
    {   
        NSMutableString *stringToWrite = [[NSMutableString alloc] init];
        [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[arAddressData valueForKey:@"firstName"]]];
        [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[arAddressData valueForKey:@"lastName"]]];
        [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[arAddressData valueForKey:@"jobTitle"]]];
        //[stringToWrite appendString:@"fname, lname, title, company, phonetype1, value1,phonetype2,value,phonetype3,value3phonetype4,value4,phonetype5,value5,phonetype6,value6,phonetype7,value7,phonetype8,value8,phonetype9,value9,phonetype10,value10,email1type,email1value,email2type,email2value,email3type,email3??value,email4type,email4value,email5type,email5value,website1,webs??ite2,website3"]; 
        NSMutableArray *arPhone = (NSMutableArray*) [arAddressData valueForKey:@"Phone"];
        for(int i = 0 ;i<[arPhone count];i++)
        {
            NSMutableDictionary *temp = (NSMutableDictionary*) [arPhone objectAtIndex:i];
            [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[temp valueForKey:@"phoneNumber"]]];
            [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[temp valueForKey:@"phoneNumber"]]];
            [temp release];
        }
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *documentDirectory=[paths objectAtIndex:0];
        NSString *strBackupFileLocation = [NSString stringWithFormat:@"%@/%@", documentDirectory,@"ContactList.csv"];
        [stringToWrite writeToFile:strBackupFileLocation atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
Run Code Online (Sandbox Code Playgroud)

  • 这个代码有点错误,不起作用 - 你有没有测试过这个?一个错误是myAddressBook在循环外定义,其值在循环内设置 - 因此每个新联系人都会覆盖前一个联系人的所有值. (3认同)

n13*_*n13 9

我使用上面的iApple代码作为起点并从中创建了一个工作版本 - 这个只收集数组中的所有地址簿条目.如上所述,最初的iApple不起作用,其中有一些错误.这个工作,并经过测试.

注意:这不会返回任何没有设置名称的联系人 - 您可以删除自己的代码,我只是这样做,因为我只需要设置名称的联系人,NSMutableDictionary不喜欢nil条目(崩溃) ).

在我自己的地址簿中,我有一些条目只是一封电子邮件 - 我不确定它们是如何到达那里的,但是当然可以使用没有名字的地址簿条目.迭代地址簿时请记住这一点.

我按照Apple的建议使用全名 - ABRecordCopyCompositeName按照用户指定的顺序返回名字和姓氏的组合.

最后,我将其设为静态方法并将其放在辅助类中.

这适用于ARC!

// returns an array of dictionaries
// each dictionary has values: fullName, phoneNumbers, emails
// fullName is a string
// phoneNumbers is an array of strings
// emails is an array of strings
+ (NSArray *)collectAddressBookContacts {
    NSMutableArray *allContacts = [[NSMutableArray alloc] init];
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef people  = ABAddressBookCopyArrayOfAllPeople(addressBook);
    for(int i = 0;i<ABAddressBookGetPersonCount(addressBook);i++)
    {
        NSMutableDictionary *aPersonDict = [[NSMutableDictionary alloc] init];
        ABRecordRef ref = CFArrayGetValueAtIndex(people, i);
        NSString *fullName = (__bridge NSString *) ABRecordCopyCompositeName(ref);
        if (fullName) {
            [aPersonDict setObject:fullName forKey:@"fullName"];

            // collect phone numbers
            NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
            ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
            for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) {
                NSString *phoneNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phones, j);
                [phoneNumbers addObject:phoneNumber];
            }
            [aPersonDict setObject:phoneNumbers forKey:@"phoneNumbers"];

            // collect emails - key "emails" will contain an array of email addresses
            ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
            NSMutableArray *emailAddresses = [[NSMutableArray alloc] init];
            for(CFIndex idx = 0; idx < ABMultiValueGetCount(emails); idx++) {
                NSString *email = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, idx);
                [emailAddresses addObject:email];
            }
            [aPersonDict setObject:emailAddresses forKey:@"emails"];
            // if you want to collect any other info that's stored in the address book, it follows the same pattern.
            // you just need the right kABPerson.... property.

            [allContacts addObject:aPersonDict];
        } else {
            // Note: I have a few entries in my phone that don't have a name set
            // Example one could have just an email address in their address book.
        }
    }
    return allContacts;
}
Run Code Online (Sandbox Code Playgroud)