Hey Friends i got all contacts detail of my iphone by using this function ..
-(void)collectContacts
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
//NSLog(@" the Name %@%",people);
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);
NSLog(@" the phoneType %@%",firstName);
NSLog(@" the phoneType %@%",lastName);
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;
NSLog(@" the phoneType %@%",phoneLabel);
NSLog(@" the phoneNumber %@%",phoneNumber);
}
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);
NSLog(@" Lable %@%",readableLabel);
address = ABMultiValueCopyValueAtIndex(multi, i);
NSLog(@" Address %@%",address);
CFRelease(address);
CFRelease(label);
}
ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
for(CFIndex idx = 0; idx < ABMultiValueGetCount(emails); idx++)
{
CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, idx);
NSString *strLbl = (NSString*) ABAddressBookCopyLocalizedLabel (ABMultiValueCopyLabelAtIndex (emails, idx));
NSLog(@" Email Lable %@%",strLbl);
NSString *strEmail_old = (NSString*)emailRef;
NSLog(@" Email-Id %@%",strEmail_old);
//[[strEmail_old componentsJoinedByString:@","] writeToFile:@"components.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
}
//[[ContactsText componentsJoinedByString:@","] writeToFile:@"components.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
// NSLog(@" Data in the .CSV file = %@%",ContactsText);
}
'
Now i want to make .csv file from this function's output .. and m getting all data in NSLog . I juat want to write .csv file from that data .. Can anyone help me in that ?? Data coming are proper but .csv file not gonna written ...Thanks
There are many Open Source extensions that add csv support to arrays. For example CHCSVParser which is available on GitHub.
Notice that your data may contain characters that require special treatment - in the case of a CSV file most notably the comma itself. If there is a comma in the string you write out, you should put double quotes around that field. In that case, if there is already a double quote in the string, you need to escape that, too.
I would suggest that you use the
CHCSVParser
from Dave DeLong, you can find it here: http://davedelong.com/portfolioIt is worth the trouble to import a good third-party framework to make sure edge cases are handled correctly.
Assuming you will have all the data in "ContactsText", try following code:
Remember that you cannot just save "components.csv" file. You have to save the file in documents folder only.