From 7615761f4c2df9482eb4e18e62d7d6ee5635b698 Mon Sep 17 00:00:00 2001 From: yair levi Date: Sat, 4 Jul 2015 21:12:28 +0300 Subject: [PATCH 1/7] Adding reference to the original object from the ABContact --- Pod/Core/APContact.h | 1 + Pod/Core/APContact.m | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Pod/Core/APContact.h b/Pod/Core/APContact.h index 4dc1745..f699ed0 100755 --- a/Pod/Core/APContact.h +++ b/Pod/Core/APContact.h @@ -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; diff --git a/Pod/Core/APContact.m b/Pod/Core/APContact.m index ed7b118..c186c64 100755 --- a/Pod/Core/APContact.m +++ b/Pod/Core/APContact.m @@ -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) { From 343486353f2c9e6240e309a6f430a4cc6b4f5b6c Mon Sep 17 00:00:00 2001 From: yair levi Date: Sun, 5 Jul 2015 19:07:27 +0300 Subject: [PATCH 2/7] Adding a method to get the VCard of a APContact --- Pod/Core/APAddressBook.h | 1 + Pod/Core/APAddressBook.m | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/Pod/Core/APAddressBook.h b/Pod/Core/APAddressBook.h index 2b96271..09e0bd2 100755 --- a/Pod/Core/APAddressBook.h +++ b/Pod/Core/APAddressBook.h @@ -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:(APContact *)contacts withImage:(BOOL)copyImage; - (void)loadContacts:(void (^)(NSArray *contacts, NSError *error))completionBlock; - (void)loadContactsOnQueue:(dispatch_queue_t)queue diff --git a/Pod/Core/APAddressBook.m b/Pod/Core/APAddressBook.m index f7db5e9..501d87c 100755 --- a/Pod/Core/APAddressBook.m +++ b/Pod/Core/APAddressBook.m @@ -95,6 +95,46 @@ + (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"]; + } + } + + NSArray* naitiveContacts = [contacts valueForKey: @"recordID"]; + 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 +{ + NSArray* allLinedStrings = + [vcard componentsSeparatedByCharactersInSet: + [NSCharacterSet newlineCharacterSet]]; + + NSMutableArray* vcardLinesWithoutPhotos = [[NSMutableArray alloc]init]; + + for (NSString *line in allLinedStrings){ + if (![line hasPrefix:@"PHOTO"]) { + [vcardLinesWithoutPhotos addObject:line]; + } + } + + return [vcardLinesWithoutPhotos componentsJoinedByString: @"/n"]; +} + - (void)loadContacts:(void (^)(NSArray *contacts, NSError *error))completionBlock { [self loadContactsOnQueue:dispatch_get_main_queue() completion:completionBlock]; From 5d0af5d00e711d27baefac1e5f19fd36e55a63a9 Mon Sep 17 00:00:00 2001 From: yair levi Date: Sun, 5 Jul 2015 19:15:27 +0300 Subject: [PATCH 3/7] update pod spec to 0.1.12 --- APAddressBook.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/APAddressBook.podspec b/APAddressBook.podspec index 57c0419..97b2353 100644 --- a/APAddressBook.podspec +++ b/APAddressBook.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "APAddressBook" - s.version = "0.1.11" + s.version = "0.1.12" s.summary = "Easy access to iOS address book" s.homepage = "https://github.com/Alterplay/APAddressBook" s.license = { :type => 'MIT', :file => 'LICENSE.txt' } From 07bfeb04746e581020be47bcc4bcec49850c66fa Mon Sep 17 00:00:00 2001 From: yair levi Date: Sun, 5 Jul 2015 19:24:29 +0300 Subject: [PATCH 4/7] fixing method definition in the header --- Pod/Core/APAddressBook.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pod/Core/APAddressBook.h b/Pod/Core/APAddressBook.h index 09e0bd2..e1181a5 100755 --- a/Pod/Core/APAddressBook.h +++ b/Pod/Core/APAddressBook.h @@ -21,7 +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:(APContact *)contacts withImage:(BOOL)copyImage; ++ (NSString *)getContactsVcard:(NSArray *)contacts withImage:(BOOL)copyImage; - (void)loadContacts:(void (^)(NSArray *contacts, NSError *error))completionBlock; - (void)loadContactsOnQueue:(dispatch_queue_t)queue From 631c534b945eadc0cd851792ebbc5915a3499b4d Mon Sep 17 00:00:00 2001 From: yair levi Date: Sun, 5 Jul 2015 19:24:49 +0300 Subject: [PATCH 5/7] Revert "update pod spec to 0.1.12" This reverts commit 5d0af5d00e711d27baefac1e5f19fd36e55a63a9. --- APAddressBook.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/APAddressBook.podspec b/APAddressBook.podspec index 97b2353..57c0419 100644 --- a/APAddressBook.podspec +++ b/APAddressBook.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "APAddressBook" - s.version = "0.1.12" + s.version = "0.1.11" s.summary = "Easy access to iOS address book" s.homepage = "https://github.com/Alterplay/APAddressBook" s.license = { :type => 'MIT', :file => 'LICENSE.txt' } From e4013b23c238b15703c13b4f1c4e9ced1ed250c3 Mon Sep 17 00:00:00 2001 From: yair levi Date: Sun, 5 Jul 2015 19:29:46 +0300 Subject: [PATCH 6/7] getting the ref object and not the record id --- Pod/Core/APAddressBook.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pod/Core/APAddressBook.m b/Pod/Core/APAddressBook.m index 501d87c..201d2b0 100755 --- a/Pod/Core/APAddressBook.m +++ b/Pod/Core/APAddressBook.m @@ -107,7 +107,7 @@ + (NSString *)getContactsVcard:(NSArray *)contacts withImage:(BOOL)copyImage } } - NSArray* naitiveContacts = [contacts valueForKey: @"recordID"]; + NSArray* naitiveContacts = [contacts valueForKey: @"originalABRecord"]; CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople((__bridge CFArrayRef)(naitiveContacts)); NSString *vcardString = [[NSString alloc] initWithData:(__bridge NSData *)vcards encoding:NSUTF8StringEncoding]; From 86c35b48c9bb28f649bf00a988114e70e5390095 Mon Sep 17 00:00:00 2001 From: yair levi Date: Sun, 5 Jul 2015 20:08:27 +0300 Subject: [PATCH 7/7] Fixing the way we trim the photo part --- Pod/Core/APAddressBook.m | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/Pod/Core/APAddressBook.m b/Pod/Core/APAddressBook.m index 201d2b0..f1cc64f 100755 --- a/Pod/Core/APAddressBook.m +++ b/Pod/Core/APAddressBook.m @@ -107,7 +107,11 @@ + (NSString *)getContactsVcard:(NSArray *)contacts withImage:(BOOL)copyImage } } - NSArray* naitiveContacts = [contacts valueForKey: @"originalABRecord"]; + 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]; @@ -120,19 +124,31 @@ + (NSString *)getContactsVcard:(NSArray *)contacts withImage:(BOOL)copyImage + (NSString *)removeImageFromVcardString:(NSString*)vcard { - NSArray* allLinedStrings = - [vcard componentsSeparatedByCharactersInSet: - [NSCharacterSet newlineCharacterSet]]; + NSScanner *scanner = [NSScanner scannerWithString:vcard]; + NSString *text = nil; - NSMutableArray* vcardLinesWithoutPhotos = [[NSMutableArray alloc]init]; + NSString *photo = @"PHOTO"; + NSString *endVcard = @"END:VCARD"; + NSString *socialProfile = @"X-SOCIALPROFILE"; - for (NSString *line in allLinedStrings){ - if (![line hasPrefix:@"PHOTO"]) { - [vcardLinesWithoutPhotos addObject:line]; + 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 [vcardLinesWithoutPhotos componentsJoinedByString: @"/n"]; + return vcard; } - (void)loadContacts:(void (^)(NSArray *contacts, NSError *error))completionBlock