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

时间:2023-01-17 14:25:58

I want to get contact details in an iPhone with information like First Name, Last Name, Phone Number, Phone Number Type, Email Address, Email Address Type etc..

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

Can anyone help me with that?

任何人都可以帮助我吗?

I want to make a .csv file out of the contact details in a particular iPhone. I want to fetch iPhone address book data.

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

4 个解决方案

#1


13  

Following is the code to get all informations of iPhone contact book...

以下是获取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];
    }

#2


9  

I used iApple's code above as a starting point and created a working version from it - this one just collects all address book entries in an array. As mentioned above the original iApple doesn't work, there's a few bugs in it. This one works, and was tested.

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

Note: This doesn't return any contacts that don't have a name set - you can remove that for your own code, I just did it because I only need contacts with names set, and NSMutableDictionary doesn't like nil entries (crashes).

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

In my own address book I have a few entries that are just an email - I am not sure how they got there, but it's certainly possible to have address book entries without a name. Keep that in mind when iterating over an address book.

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

I am using the full name as per Apple's recommendations - ABRecordCopyCompositeName returns a composite of first and last name in the order specified by the user.

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

Finally, I made this a static method and put it in a helper class.

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

This is for use with ARC!

这适用于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;
}

#3


2  

First you will need to use the address book framework so this must be added to your Xcode project.

首先,您需要使用地址簿框架,因此必须将其添加到Xcode项目中。

Next you will need to break the task down into a couple steps.

接下来,您需要将任务分解为几个步骤。

1) Get the people inside the address book

1)让人们进入地址簿

2) Create your .csv file. I'm assuming you know something about CSV file formatting using characters to separate fields and when to add return characters so you have a properly formatted file. This is probably left for another question thread if you need help with this.

2)创建.csv文件。我假设你知道一些关于CSV文件格式化的信息,使用字符分隔字段以及何时添加返回字符,以便你有一个格式正确的文件。如果您需要帮助,这可能留给另一个问题线程。

3) Save your .csv file somewhere

3)将.csv文件保存在某个地方

1) To get an array of all people in the address book you would do something like the following. The reference documentation for ABAddressBook is here. It should be very helpful in helping you access the data.

1)要获得地址簿中所有人的数组,您将执行以下操作。 ABAddressBook的参考文档在这里。它应该对帮助您访问数据非常有帮助。

ABAddressBook *sharedBook = [ABAddressBook sharedAddressBook];
NSArray *peopleList = [sharedBook people];

2) You will have to iterate through each of the people and build your overall csv data. Usually you would manually create the csv data in an NSString and then convert it to NSData and save the NSData to a file. This is not ideal if you are dealing with a really large set of data. If this is the case then you would probably want some code to write your csv data to the file in chunks so you can free memory as you go. For simplicity sake my code just shows you creating the full file then saving the whole works.

2)您将不得不遍历每个人并构建您的整体csv数据。通常,您将在NSString中手动创建csv数据,然后将其转换为NSData并将NSData保存到文件中。如果您处理的是大量数据,这并不理想。如果是这种情况,那么您可能需要一些代码将csv数据以块的形式写入文件,这样您就可以随时释放内存。为简单起见,我的代码只显示您创建完整文件,然后保存整个工作。

NSString *csvString = @"";
for(ABPerson *aPerson in peopleList) {
  //Do something here to write each property you want to the CSV file.
  csvString = [csvString stringByAppendingFormat:@"'%@'," 
                                    [aPerson valueForProperty:kABFirstNameProperty]];
}

NSData *csvData = [csvString dataUsingEncoding:NSUTF8StringEncoding];

3) Write you file to somewhere

3)把你的文件写到某个地方

//This is an example of writing your csv data to a file that will be saved in the application's sand box directory. 
//This file could be extracted using iTunes file sharing.

//Get the proper path to save the file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"my_file.csv"];

 //Actually write the data
 BOOL isSuccessful = [csvData writeToFile:fullPath atomically:NO];
 if(isSuccessful) {
     //Do something if the file was written
  } else {
    //Do something if there was an error writing the file
  }

#4


0  

See Adress Book API particulary Importing and Exporting Person and Group Records

请参阅Adress Book API,特别是导入和导出人员和组记录

chack also the Address Book Test example in this blog

chack也是这个博客中的地址簿测试示例

#1


13  

Following is the code to get all informations of iPhone contact book...

以下是获取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];
    }

#2


9  

I used iApple's code above as a starting point and created a working version from it - this one just collects all address book entries in an array. As mentioned above the original iApple doesn't work, there's a few bugs in it. This one works, and was tested.

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

Note: This doesn't return any contacts that don't have a name set - you can remove that for your own code, I just did it because I only need contacts with names set, and NSMutableDictionary doesn't like nil entries (crashes).

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

In my own address book I have a few entries that are just an email - I am not sure how they got there, but it's certainly possible to have address book entries without a name. Keep that in mind when iterating over an address book.

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

I am using the full name as per Apple's recommendations - ABRecordCopyCompositeName returns a composite of first and last name in the order specified by the user.

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

Finally, I made this a static method and put it in a helper class.

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

This is for use with ARC!

这适用于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;
}

#3


2  

First you will need to use the address book framework so this must be added to your Xcode project.

首先,您需要使用地址簿框架,因此必须将其添加到Xcode项目中。

Next you will need to break the task down into a couple steps.

接下来,您需要将任务分解为几个步骤。

1) Get the people inside the address book

1)让人们进入地址簿

2) Create your .csv file. I'm assuming you know something about CSV file formatting using characters to separate fields and when to add return characters so you have a properly formatted file. This is probably left for another question thread if you need help with this.

2)创建.csv文件。我假设你知道一些关于CSV文件格式化的信息,使用字符分隔字段以及何时添加返回字符,以便你有一个格式正确的文件。如果您需要帮助,这可能留给另一个问题线程。

3) Save your .csv file somewhere

3)将.csv文件保存在某个地方

1) To get an array of all people in the address book you would do something like the following. The reference documentation for ABAddressBook is here. It should be very helpful in helping you access the data.

1)要获得地址簿中所有人的数组,您将执行以下操作。 ABAddressBook的参考文档在这里。它应该对帮助您访问数据非常有帮助。

ABAddressBook *sharedBook = [ABAddressBook sharedAddressBook];
NSArray *peopleList = [sharedBook people];

2) You will have to iterate through each of the people and build your overall csv data. Usually you would manually create the csv data in an NSString and then convert it to NSData and save the NSData to a file. This is not ideal if you are dealing with a really large set of data. If this is the case then you would probably want some code to write your csv data to the file in chunks so you can free memory as you go. For simplicity sake my code just shows you creating the full file then saving the whole works.

2)您将不得不遍历每个人并构建您的整体csv数据。通常,您将在NSString中手动创建csv数据,然后将其转换为NSData并将NSData保存到文件中。如果您处理的是大量数据,这并不理想。如果是这种情况,那么您可能需要一些代码将csv数据以块的形式写入文件,这样您就可以随时释放内存。为简单起见,我的代码只显示您创建完整文件,然后保存整个工作。

NSString *csvString = @"";
for(ABPerson *aPerson in peopleList) {
  //Do something here to write each property you want to the CSV file.
  csvString = [csvString stringByAppendingFormat:@"'%@'," 
                                    [aPerson valueForProperty:kABFirstNameProperty]];
}

NSData *csvData = [csvString dataUsingEncoding:NSUTF8StringEncoding];

3) Write you file to somewhere

3)把你的文件写到某个地方

//This is an example of writing your csv data to a file that will be saved in the application's sand box directory. 
//This file could be extracted using iTunes file sharing.

//Get the proper path to save the file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"my_file.csv"];

 //Actually write the data
 BOOL isSuccessful = [csvData writeToFile:fullPath atomically:NO];
 if(isSuccessful) {
     //Do something if the file was written
  } else {
    //Do something if there was an error writing the file
  }

#4


0  

See Adress Book API particulary Importing and Exporting Person and Group Records

请参阅Adress Book API,特别是导入和导出人员和组记录

chack also the Address Book Test example in this blog

chack也是这个博客中的地址簿测试示例