BEGIN:VCARD
VERSION:2.1
N:Mel;Gaga
FN:Gaga Mel
ORG:Smerpe
TEL;CELL;VOICE:111122223333
TEL;WORK;VOICE:111122334
TEL;HOME;VOICE:1 (234) 123-4
TEL;WORK;VOICE:(999) 999-999
ADR;HOME:;;Asddff Rrttygfd 123-2;Suwon;GYEONGGI;443803;United States
LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Asddff Rrttygfd 123-2, Suwon GYEONGGI 443803 United States
URL;HOME:http://www.aaaa.com
URL;HOME:http://aaa.bbbb.com
URL;WORK:http://work.ggg.com
URL;WORK:http://Other.gggg.com
EMAIL;PREF;INTERNET:aaaa@bbbb.com
EMAIL;PREF;INTERNET:ddddd@ggggg.com
REV:20101228T095131Z
END:VCARD
----------------------------------------------------
BEGIN:VCARD
VERSION:3.0
N:Mel;Gaga
FN:Gaga Mel
ORG:Smerpe
TEL;TYPE=CELL:111122223333
TEL;TYPE=WORK:111122334
TEL;TYPE=HOME:1 (234) 123-4
TEL;TYPE=WORK:(999) 999-999
ADR;TYPE=HOME:;;Asddff Rrttygfd 123-2;Suwon;GYEONGGI;443803;United States
LABEL;TYPE=HOME:;;Asddff Rrttygfd 123-2, Suwon GYEONGGI 443803 United States
URL;TYPE=HOME:HTTP://www.aaaa.com
URL;TYPE=HOME:HTTP://aaa.bbbb.com
URL;TYPE=WORK:HTTP://work.ggg.com
URL;TYPE=WORK:HTTP://Other.gggg.com
EMAIL;TYPE=PREF,INTERNET:aaaa@bbbb.com
EMAIL;TYPE=PREF,INTERNET:ddddd@ggggg.com
REV:20101228T095131Z
END:VCARD
==================================================================================
-(NSString *)makeVCard:(NSInteger *)record_ID
{
// Read Address DB with recordID
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook,(ABRecordID)record_ID);
NSLog(@"person = %@",person);
// vCard version
// Now using v2.1
NSString *version = @"2.1"; // @"2.1" or @"3.0"
#pragma mark VCard v2.1
// version 2.1
if ([version isEqualToString:@"2.1"]) {
// Make VCard from Format v2.1
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
// property Type and Version
[mutableArray addObject:@"BEGIN:VCARD"];
[mutableArray addObject:@"VERSION:2.1"];
//Name : N , FN
[mutableArray addObject:[NSString stringWithFormat:@"N:%@;%@",
ABRecordCopyValue(person, kABPersonLastNameProperty),
ABRecordCopyValue(person, kABPersonFirstNameProperty)]];
[mutableArray addObject:[NSString stringWithFormat:@"FN:%@",
ABRecordCopyCompositeName(person)]];
// Company Name : ORG
NSString *strTemp = nil;
strTemp = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
if (strTemp != nil) {
[mutableArray addObject:[NSString stringWithFormat:@"ORG:%@", strTemp]];
}
// Job Title : TITLE
strTemp = nil;
strTemp = (NSString *)ABRecordCopyValue(person, kABPersonJobTitleProperty);
if (strTemp) {
[mutableArray addObject:[NSString stringWithFormat:@"TITLE:%@", strTemp]];
}
// PHOTO
// skip to this
// Phone Number // TEL, TYPE
// TEL;WORK;VOICE:xxxxxxxx
// TEL;HOME;VOICE:xxxxxxxx
// TEL;CELL;VOICE:xxxxxxxx
// TEL;WORK;FAX:xxxxxxxx <= Not exist in Iphone,, delete it
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* telNumber=@"";
NSString* telType;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
telType = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
// mobile
if([telType isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
[telNumber release] ;
telNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[mutableArray addObject:[NSString stringWithFormat:@"TEL;CELL;VOICE:%@", telNumber]];
}
// iphone => WORK 로 저장
else if ([telType isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
{
[telNumber release] ;
telNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[mutableArray addObject:[NSString stringWithFormat:@"TEL;WORK;VOICE:%@", telNumber]];
}
// home
else if ([telType isEqualToString:@"_$!<Home>!$_"])
{
[telNumber release] ;
telNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[mutableArray addObject:[NSString stringWithFormat:@"TEL;HOME;VOICE:%@", telNumber]];
}
// work
else if ([telType isEqualToString:@"_$!<Work>!$_"])
{
[telNumber release] ;
telNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[mutableArray addObject:[NSString stringWithFormat:@"TEL;WORK;VOICE:%@", telNumber]];
}
// NSLog(@"Label = %@",telType);
// NSLog(@"mobile = %@",telNumber);
}
[telNumber release];
[telType release];
// Address : ADR
// Label of Street : LABEL
ABMultiValueRef streets = ABRecordCopyValue(person, kABPersonAddressProperty);
for (CFIndex j = 0; j < ABMultiValueGetCount(streets); j++) {
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(streets, j);
NSString *streetType = (NSString*)ABMultiValueCopyLabelAtIndex(streets, j);
NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
NSString *streetLabel = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy];
NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy];
NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy];
NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy];
// change character '\n' to space
if([street rangeOfString:@"\n"].location != NSNotFound) {
NSArray* removeNewLineStr = [street componentsSeparatedByString:@"\n"];
NSString *changeString = nil;
changeString = [NSString stringWithFormat:@"%@", [removeNewLineStr objectAtIndex:0 ]];
for(int index=1; index < removeNewLineStr.count ; index++)
{
changeString = [NSString stringWithFormat:@"%@ %@",changeString, [removeNewLineStr objectAtIndex:index]];
}
[street release];
street = [NSString stringWithFormat:@"%@",changeString];
[changeString release];
}
// address : ADR, Label
if ([streetType isEqualToString:@"_$!<Home>!$_"]) {
[mutableArray addObject:[NSString stringWithFormat:@"ADR;HOME:;;%@;%@;%@;%@;%@",street, city, state, zip, country]];
[mutableArray addObject:[NSString stringWithFormat:@"LABEL;HOME;ENCODING=QUOTED-PRINTABLE:%@, %@ %@ %@ %@",street, city, state, zip, country]];
}
else if([streetType isEqualToString:@"_$!<Work>!$_"]) {
[mutableArray addObject:[NSString stringWithFormat:@"ADR;WORK:;;%@;%@;%@;%@;%@",street, city, state, zip, country]];
[mutableArray addObject:[NSString stringWithFormat:@"LABEL;WORK;ENCODING=QUOTED-PRINTABLE:%@, %@ %@ %@ %@",street, city, state, zip, country]];
}
//release
[street release];
[streetLabel release];
[city release];
[state release];
[zip release];
[country release];
CFRelease(dict);
CFRelease(streetType);
}
// URL : URL address
ABMultiValueRef personURL =(NSString*)ABRecordCopyValue(person, kABPersonURLProperty);
NSString* dataURL=@"";
NSString* TypeURL;
for(CFIndex i = 0; i < ABMultiValueGetCount(personURL); i++) {
TypeURL = (NSString*)ABMultiValueCopyLabelAtIndex(personURL, i);
// homepage
if ([TypeURL isEqualToString:@"_$!<HomePage>!$_"])
{
[dataURL release] ;
dataURL = (NSString*)ABMultiValueCopyValueAtIndex(personURL, i);
[mutableArray addObject:[NSString stringWithFormat:@"URL;HOME:http://%@", dataURL]];
}
// home
else if ([TypeURL isEqualToString:@"_$!<Home>!$_"])
{
[dataURL release] ;
dataURL = (NSString*)ABMultiValueCopyValueAtIndex(personURL, i);
[mutableArray addObject:[NSString stringWithFormat:@"URL;HOME:http://%@", dataURL]];
}
// work
else if ([TypeURL isEqualToString:@"_$!<Work>!$_"])
{
[dataURL release] ;
dataURL = (NSString*)ABMultiValueCopyValueAtIndex(personURL, i);
[mutableArray addObject:[NSString stringWithFormat:@"URL;WORK:http://%@", dataURL]];
}
// Other
else if ([TypeURL isEqualToString:@"_$!<Other>!$_"])
{
[dataURL release] ;
dataURL = (NSString*)ABMultiValueCopyValueAtIndex(personURL, i);
[mutableArray addObject:[NSString stringWithFormat:@"URL;WORK:http://%@", dataURL]];
}
// NSLog(@"URL type = %@",TypeURL);
// NSLog(@"URL address = %@", dataURL);
}
[TypeURL release];
[dataURL release];
//Email : EMAIL
ABMultiValueRef personEmail =(NSString*)ABRecordCopyValue(person, kABPersonEmailProperty);
NSString* dataEmail=@"";
NSString* TypeEmail;
for(CFIndex i = 0; i < ABMultiValueGetCount(personEmail); i++) {
TypeEmail = (NSString*)ABMultiValueCopyLabelAtIndex(personEmail, i);
// home
if ([TypeEmail isEqualToString:@"_$!<Home>!$_"])
{
[dataEmail release] ;
dataEmail = (NSString*)ABMultiValueCopyValueAtIndex(personEmail, i);
[mutableArray addObject:[NSString stringWithFormat:@"EMAIL;PREF;INTERNET:%@", dataEmail]];
}
// work
else if ([TypeEmail isEqualToString:@"_$!<Work>!$_"])
{
[dataEmail release] ;
dataEmail = (NSString*)ABMultiValueCopyValueAtIndex(personEmail, i);
[mutableArray addObject:[NSString stringWithFormat:@"EMAIL;PREF;INTERNET:%@", dataEmail]];
}
}
[dataEmail release];
[TypeEmail release];
// Last Revision : REV (Combination of the calendar date and time of day of the last update to the vCard Object)
CFDateRef modifyDate = ABRecordCopyValue(person, kABPersonModificationDateProperty);
NSDateFormatter *dateFormatter = nil;
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"yyyyMMdd'T'HHmmss'Z'"];
NSString *m_modifyDay = [dateFormatter stringFromDate:(NSDate *)modifyDate];
// NSLog(@"modifyDaty = %@", m_modifyDay);
[mutableArray addObject:[NSString stringWithFormat:@"REV:%@", m_modifyDay]];
[dateFormatter release];
[m_modifyDay release];
CFRelease(modifyDate);
// END Label
[mutableArray addObject:@"END:VCARD"];
NSString *string = [mutableArray componentsJoinedByString:@"\n"];
[mutableArray release];
NSLog(@"%@",string);
return string;
}
else {
#pragma mark VCard v3.0
// version 3.0
// Make VCard from Format v3.0
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
// property Type and Version
[mutableArray addObject:@"BEGIN:VCARD"];
[mutableArray addObject:@"VERSION:3.0"];
//Name : N , FN
[mutableArray addObject:[NSString stringWithFormat:@"N:%@;%@",
ABRecordCopyValue(person, kABPersonLastNameProperty),
ABRecordCopyValue(person, kABPersonFirstNameProperty)]];
[mutableArray addObject:[NSString stringWithFormat:@"FN:%@",
ABRecordCopyCompositeName(person)]];
// Company Name : ORG
NSString *strTemp = nil;
strTemp = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
if (strTemp != nil) {
[mutableArray addObject:[NSString stringWithFormat:@"ORG:%@", strTemp]];
}
// Job Title : TITLE
strTemp = nil;
strTemp = (NSString *)ABRecordCopyValue(person, kABPersonJobTitleProperty);
if (strTemp) {
[mutableArray addObject:[NSString stringWithFormat:@"TITLE:%@", strTemp]];
}
// PHOTO
// skip to this
// Phone Number // TEL, TYPE
// TEL;TYPE=WORK,VOICE:xxxxxxxx
// TEL;TYPE=HOME,VOICE:xxxxxxxx
// TEL;TYPE=CELL,VOICE:xxxxxxxx
// TEL;TYPE=WORK,FAX:xxxxxxxx <= Not exist in Iphone,, delete it
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* telNumber=@"";
NSString* telType;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
telType = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
// mobile
if([telType isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
[telNumber release] ;
telNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[mutableArray addObject:[NSString stringWithFormat:@"TEL;TYPE=CELL,VOICE:%@", telNumber]];
}
// iphone => WORK 로 저장
else if ([telType isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
{
[telNumber release] ;
telNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[mutableArray addObject:[NSString stringWithFormat:@"TEL;TYPE=WORK,VOICE:%@", telNumber]];
}
// home
else if ([telType isEqualToString:@"_$!<Home>!$_"])
{
[telNumber release] ;
telNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[mutableArray addObject:[NSString stringWithFormat:@"TEL;TYPE=HOME,VOICE:%@", telNumber]];
}
// work
else if ([telType isEqualToString:@"_$!<Work>!$_"])
{
[telNumber release] ;
telNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[mutableArray addObject:[NSString stringWithFormat:@"TEL;TYPE=WORK,VOICE:%@", telNumber]];
}
// NSLog(@"Label = %@",telType);
// NSLog(@"mobile = %@",telNumber);
}
[telNumber release];
[telType release];
// Address : ADR
// Label of Street : LABEL
ABMultiValueRef streets = ABRecordCopyValue(person, kABPersonAddressProperty);
for (CFIndex j = 0; j < ABMultiValueGetCount(streets); j++) {
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(streets, j);
NSString *streetType = (NSString*)ABMultiValueCopyLabelAtIndex(streets, j);
NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
NSString *streetLabel = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy];
NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy];
NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy];
NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy];
// change character '\n' to space
if([street rangeOfString:@"\n"].location != NSNotFound) {
NSArray* removeNewLineStr = [street componentsSeparatedByString:@"\n"];
NSString *changeString = nil;
changeString = [NSString stringWithFormat:@"%@", [removeNewLineStr objectAtIndex:0 ]];
for(int index=1; index < removeNewLineStr.count ; index++)
{
changeString = [NSString stringWithFormat:@"%@ %@",changeString, [removeNewLineStr objectAtIndex:index]];
}
[street release];
street = [NSString stringWithFormat:@"%@",changeString];
[changeString release];
}
// address : ADR, Label
if ([streetType isEqualToString:@"_$!<Home>!$_"]) {
[mutableArray addObject:[NSString stringWithFormat:@"ADR;TYPE=HOME:;;%@;%@;%@;%@;%@",street, city, state, zip, country]];
[mutableArray addObject:[NSString stringWithFormat:@"LABEL;TYPE=HOME:;;%@, %@ %@ %@ %@",street, city, state, zip, country]];
}
else if([streetType isEqualToString:@"_$!<Work>!$_"]) {
[mutableArray addObject:[NSString stringWithFormat:@"ADR;TYPE=WORK:;;%@;%@;%@;%@;%@",street, city, state, zip, country]];
[mutableArray addObject:[NSString stringWithFormat:@"LABEL;TYPE=WORK:;;%@, %@ %@ %@ %@",street, city, state, zip, country]];
}
//release
[street release];
[streetLabel release];
[city release];
[state release];
[zip release];
[country release];
CFRelease(dict);
CFRelease(streetType);
}
// URL : URL address
ABMultiValueRef personURL =(NSString*)ABRecordCopyValue(person, kABPersonURLProperty);
NSString* dataURL=@"";
NSString* TypeURL;
for(CFIndex i = 0; i < ABMultiValueGetCount(personURL); i++) {
TypeURL = (NSString*)ABMultiValueCopyLabelAtIndex(personURL, i);
// homepage
if ([TypeURL isEqualToString:@"_$!<HomePage>!$_"])
{
[dataURL release] ;
dataURL = (NSString*)ABMultiValueCopyValueAtIndex(personURL, i);
[mutableArray addObject:[NSString stringWithFormat:@"URL;TYPE=HOME:HTTP://%@", dataURL]];
}
// home
else if ([TypeURL isEqualToString:@"_$!<Home>!$_"])
{
[dataURL release] ;
dataURL = (NSString*)ABMultiValueCopyValueAtIndex(personURL, i);
[mutableArray addObject:[NSString stringWithFormat:@"URL;TYPE=HOME:HTTP://%@", dataURL]];
}
// work
else if ([TypeURL isEqualToString:@"_$!<Work>!$_"])
{
[dataURL release] ;
dataURL = (NSString*)ABMultiValueCopyValueAtIndex(personURL, i);
[mutableArray addObject:[NSString stringWithFormat:@"URL;TYPE=WORK:HTTP://%@", dataURL]];
}
// Other
else if ([TypeURL isEqualToString:@"_$!<Other>!$_"])
{
[dataURL release] ;
dataURL = (NSString*)ABMultiValueCopyValueAtIndex(personURL, i);
[mutableArray addObject:[NSString stringWithFormat:@"URL;TYPE=WORK:HTTP://%@", dataURL]];
}
// NSLog(@"URL type = %@",TypeURL);
// NSLog(@"URL address = %@", dataURL);
}
[TypeURL release];
[dataURL release];
//Email : EMAIL
ABMultiValueRef personEmail =(NSString*)ABRecordCopyValue(person, kABPersonEmailProperty);
NSString* dataEmail=@"";
NSString* TypeEmail;
for(CFIndex i = 0; i < ABMultiValueGetCount(personEmail); i++) {
TypeEmail = (NSString*)ABMultiValueCopyLabelAtIndex(personEmail, i);
// home
if ([TypeEmail isEqualToString:@"_$!<Home>!$_"])
{
[dataEmail release] ;
dataEmail = (NSString*)ABMultiValueCopyValueAtIndex(personEmail, i);
[mutableArray addObject:[NSString stringWithFormat:@"EMAIL;TYPE=PREF,INTERNET:%@", dataEmail]];
}
// work
else if ([TypeEmail isEqualToString:@"_$!<Work>!$_"])
{
[dataEmail release] ;
dataEmail = (NSString*)ABMultiValueCopyValueAtIndex(personEmail, i);
[mutableArray addObject:[NSString stringWithFormat:@"EMAIL;TYPE=PREF,INTERNET:%@", dataEmail]];
}
}
[dataEmail release];
[TypeEmail release];
// Last Revision : REV (Combination of the calendar date and time of day of the last update to the vCard Object)
CFDateRef modifyDate = ABRecordCopyValue(person, kABPersonModificationDateProperty);
NSDateFormatter *dateFormatter = nil;
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"yyyyMMdd'T'HHmmss'Z'"];
NSString *m_modifyDay = [dateFormatter stringFromDate:(NSDate *)modifyDate];
// NSLog(@"modifyDaty = %@", m_modifyDay);
[mutableArray addObject:[NSString stringWithFormat:@"REV:%@", m_modifyDay]];
[dateFormatter release];
[m_modifyDay release];
CFRelease(modifyDate);
// END Label
[mutableArray addObject:@"END:VCARD"];
NSString *string = [mutableArray componentsJoinedByString:@"\n"];
[mutableArray release];
NSLog(@"%@",string);
return string;
}
}