This repository has been archived by the owner on Aug 2, 2019. It is now read-only.
/
VcardImporter.m
121 lines (100 loc) · 3.85 KB
/
VcardImporter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// VcardImporter.m
// AddressBookVcardImport
//
// Created by Alan Harper on 20/10/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "VcardImporter.h"
#import "BaseSixtyFour.h"
@implementation VcardImporter
- (id) init {
if (self = [super init]) {
addressBook = ABAddressBookCreate();
}
return self;
}
- (void) dealloc {
CFRelease(addressBook);
[super dealloc];
}
- (void)parse {
[self emptyAddressBook];
NSString *filename = [[NSBundle mainBundle] pathForResource:@"vCards" ofType:@"vcf"];
NSLog(@"openning file %@", filename);
NSData *stringData = [NSData dataWithContentsOfFile:filename];
NSString *vcardString = [[NSString alloc] initWithData:stringData encoding:NSUTF8StringEncoding];
NSArray *lines = [vcardString componentsSeparatedByString:@"\n"];
for(NSString* line in lines) {
[self parseLine:line];
}
ABAddressBookSave(addressBook, NULL);
[vcardString release];
}
- (void) parseLine:(NSString *)line {
if (base64image && [line hasPrefix:@" "]) {
NSString *trimmedLine = [line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
base64image = [base64image stringByAppendingString:trimmedLine];
} else if (base64image) {
// finished contatenating image string
[self parseImage];
} else if ([line hasPrefix:@"BEGIN"]) {
personRecord = ABPersonCreate();
} else if ([line hasPrefix:@"END"]) {
ABAddressBookAddRecord(addressBook,personRecord, NULL);
} else if ([line hasPrefix:@"N:"]) {
[self parseName:line];
} else if ([line hasPrefix:@"EMAIL;"]) {
[self parseEmail:line];
} else if ([line hasPrefix:@"PHOTO;BASE64"]) {
base64image = [NSString string];
}
}
- (void) parseName:(NSString *)line {
NSArray *upperComponents = [line componentsSeparatedByString:@":"];
NSArray *components = [[upperComponents objectAtIndex:1] componentsSeparatedByString:@";"];
ABRecordSetValue (personRecord, kABPersonLastNameProperty,[components objectAtIndex:0], NULL);
ABRecordSetValue (personRecord, kABPersonFirstNameProperty,[components objectAtIndex:1], NULL);
ABRecordSetValue (personRecord, kABPersonPrefixProperty,[components objectAtIndex:3], NULL);
}
- (void) parseEmail:(NSString *)line {
NSArray *mainComponents = [line componentsSeparatedByString:@":"];
NSString *emailAddress = [mainComponents objectAtIndex:1];
CFStringRef label;
ABMutableMultiValueRef multiEmail;
if ([line rangeOfString:@"WORK"].location != NSNotFound) {
label = kABWorkLabel;
} else if ([line rangeOfString:@"HOME"].location != NSNotFound) {
label = kABHomeLabel;
} else {
label = kABOtherLabel;
}
ABMultiValueRef immutableMultiEmail = ABRecordCopyValue(personRecord, kABPersonEmailProperty);
if (immutableMultiEmail) {
multiEmail = ABMultiValueCreateMutableCopy(immutableMultiEmail);
} else {
multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
}
ABMultiValueAddValueAndLabel(multiEmail, emailAddress, label, NULL);
ABRecordSetValue(personRecord, kABPersonEmailProperty, multiEmail,nil);
CFRelease(multiEmail);
if (immutableMultiEmail) {
CFRelease(immutableMultiEmail);
}
}
- (void) parseImage {
NSData *imageData = [BaseSixtyFour decode:base64image];
base64image = nil;
ABPersonSetImageData(personRecord, (CFDataRef)imageData, NULL);
}
- (void) emptyAddressBook {
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
int arrayCount = CFArrayGetCount(people);
ABRecordRef abrecord;
for (int i = 0; i < arrayCount; i++) {
abrecord = CFArrayGetValueAtIndex(people, i);
ABAddressBookRemoveRecord(addressBook,abrecord, NULL);
}
CFRelease(people);
}
@end