Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding "getContactsVcard" method to APAddressBook #82

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pod/Core/APAddressBook.h
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
+ (void)requestAccess:(void (^)(BOOL granted, NSError * error))completionBlock;
+ (void)requestAccessOnQueue:(dispatch_queue_t)queue
completion:(void (^)(BOOL granted, NSError * error))completionBlock;
+ (NSString *)getContactsVcard:(NSArray *)contacts withImage:(BOOL)copyImage;

- (void)loadContacts:(void (^)(NSArray *contacts, NSError *error))completionBlock;
- (void)loadContactsOnQueue:(dispatch_queue_t)queue
56 changes: 56 additions & 0 deletions Pod/Core/APAddressBook.m
Original file line number Diff line number Diff line change
@@ -95,6 +95,62 @@ + (void)requestAccessOnQueue:(dispatch_queue_t)queue

}

+ (NSString *)getContactsVcard:(NSArray *)contacts withImage:(BOOL)copyImage
{
if (contacts.count == 0) {
return @"";
}

for (NSUInteger i = 0; i < contacts.count; i++){
if (![contacts[i] isKindOfClass:[APContact class]]) {
[NSException raise:@"Invalid type of object" format:@"input array object is not of 'APContact' type"];
}
}

NSMutableArray *naitiveContacts = [[NSMutableArray alloc]init];
for (APContact *contact in contacts){
[naitiveContacts addObject:contact.originalABRecord];
}

CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople((__bridge CFArrayRef)(naitiveContacts));
NSString *vcardString = [[NSString alloc] initWithData:(__bridge NSData *)vcards encoding:NSUTF8StringEncoding];

if (copyImage) {
return vcardString;
}

return [self removeImageFromVcardString:vcardString];
}

+ (NSString *)removeImageFromVcardString:(NSString*)vcard
{
NSScanner *scanner = [NSScanner scannerWithString:vcard];
NSString *text = nil;

NSString *photo = @"PHOTO";
NSString *endVcard = @"END:VCARD";
NSString *socialProfile = @"X-SOCIALPROFILE";

if ([vcard rangeOfString:@"X-SOCIALPROFILE"].location == NSNotFound) {
while ([scanner isAtEnd] == NO) {
[scanner scanUpToString:photo intoString:NULL] ;
[scanner scanUpToString:endVcard intoString:&text] ;
vcard = [vcard stringByReplacingOccurrencesOfString:
[NSString stringWithFormat:@"%@", text] withString:@""];
}
}else{
while ([scanner isAtEnd] == NO) {
[scanner scanUpToString:photo intoString:NULL] ;
[scanner scanUpToString:socialProfile intoString:&text] ;
[scanner scanUpToString:endVcard intoString:NULL];
vcard = [vcard stringByReplacingOccurrencesOfString:
[NSString stringWithFormat:@"%@", text] withString:@""];
}
}

return vcard;
}

- (void)loadContacts:(void (^)(NSArray *contacts, NSError *error))completionBlock
{
[self loadContactsOnQueue:dispatch_get_main_queue() completion:completionBlock];
1 change: 1 addition & 0 deletions Pod/Core/APContact.h
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
@property (nonatomic, readonly) NSArray *socialProfiles;
@property (nonatomic, readonly) NSString *note;
@property (nonatomic, readonly) NSArray *linkedRecordIDs;
@property (nonatomic, readonly) ABRecordRef originalABRecord;

- (id)initWithRecordRef:(ABRecordRef)recordRef fieldMask:(APContactField)fieldMask;

2 changes: 2 additions & 0 deletions Pod/Core/APContact.m
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@ - (id)initWithRecordRef:(ABRecordRef)recordRef fieldMask:(APContactField)fieldMa
self = [super init];
if (self)
{
// Preserve original ABAddressBook record for future use (like generating vCard)
_originalABRecord = recordRef;
_fieldMask = fieldMask;
if (fieldMask & APContactFieldFirstName)
{