diff --git a/README.md b/README.md index 5e70744..7c41d15 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ Presenting / editing an RHPerson instance in a ABPersonViewController. Background geocoding ```objectivec - if ([RHAddressBook isGeocodingSupported){ + if ([RHAddressBook isGeocodingSupported]){ [RHAddressBook setPreemptiveGeocodingEnabled:YES]; //class method } float progress = [_addressBook preemptiveGeocodingProgress]; // 0.0f - 1.0f diff --git a/RHAddressBook.podspec b/RHAddressBook.podspec new file mode 100644 index 0000000..08003d6 --- /dev/null +++ b/RHAddressBook.podspec @@ -0,0 +1,40 @@ +Pod::Spec.new do |s| + s.name = 'RHAddressBook' + s.version = '1.2.0' + s.homepage = 'https://github.com/heardrwt/RHAddressBook' + s.summary = 'A Cocoa / Objective-C library for interfacing with the iOS AddressBook. Also adds geocoding support.' + s.author = 'Richard Heard' + s.source = { :git => 'https://github.com/heardrwt/RHAddressBook.git', :tag => s.version.to_s} + s.source_files = 'RHAddressBook/*.{h,m}' + s.prefix_header_file = 'RHAddressBook/RHAddressBook-Prefix.pch' + s.frameworks = 'AddressBook', 'CoreLocation', 'AddressBookUI' + s.platform = :ios + s.license = { + :type => 'Modified BSD', + :text => <<-LICENSE + Copyright (c) 2011-2012 Richard Heard. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + LICENSE + } +end \ No newline at end of file diff --git a/RHAddressBook/RHAddressBook.h b/RHAddressBook/RHAddressBook.h index c5e9d59..a640de2 100644 --- a/RHAddressBook/RHAddressBook.h +++ b/RHAddressBook/RHAddressBook.h @@ -105,6 +105,10 @@ typedef NS_ENUM(NSUInteger, RHAuthorizationStatus) { @property (nonatomic, readonly, copy) NSArray *peopleOrderedByFirstName; @property (nonatomic, readonly, copy) NSArray *peopleOrderedByLastName; +- (NSArray *)peopleUnifiedUsingDefaultSource; +- (NSArray *)peopleUnifiedUsingSource:(RHSource *)source; +- (NSArray *)peopleUnified; + -(NSArray*)peopleWithName:(NSString*)name; -(NSArray*)peopleWithEmail:(NSString*)email; -(RHPerson*)personForABRecordRef:(ABRecordRef)personRef; //returns nil if ref not found in the current ab, eg unsaved record from another ab. if the passed recordRef does not belong to the current addressbook, the returned person objects underlying personRef will differ from the passed in value. This is required in-order to maintain thread safety for the underlying AddressBook instance. diff --git a/RHAddressBook/RHAddressBook.m b/RHAddressBook/RHAddressBook.m index 95aaa03..f0336c7 100644 --- a/RHAddressBook/RHAddressBook.m +++ b/RHAddressBook/RHAddressBook.m @@ -645,6 +645,61 @@ -(NSArray*)peopleOrderedByLastName{ return [self peopleOrderedBySortOrdering:kABPersonSortByLastName]; } +/** + * + * + * @return NSArray + */ +- (NSArray *)peopleUnifiedUsingDefaultSource +{ + __block NSArray *result = nil; + + rh_dispatch_sync_for_addressbook(self, ^{ + result = arc_retain([self peopleUnifiedUsingSource:[self defaultSource]]); + }); + + return arc_autorelease(result); +} + +/** + * + * + * @param source + * + * @return NSArray + */ +- (NSArray *)peopleUnifiedUsingSource:(RHSource *)source +{ + __block NSArray *result = nil; + + rh_dispatch_sync_for_addressbook(self, ^{ + CFArrayRef peopleRefs = ABAddressBookCopyArrayOfAllPeopleInSource(_addressBookRef, source.recordRef); + + if (peopleRefs) { + result = arc_retain([self peopleUnifiedForABRecordRefs:peopleRefs]); + CFRelease(peopleRefs); + } + }); + + return arc_autorelease(result); +} + +- (NSArray *)peopleUnified +{ + __block NSArray *result = nil; + + rh_dispatch_sync_for_addressbook(self, ^{ + CFArrayRef peopleRefs = ABAddressBookCopyArrayOfAllPeople(_addressBookRef); + + if (peopleRefs) { + result = arc_retain([self peopleUnifiedForABRecordRefs:peopleRefs]); + CFRelease(peopleRefs); + } + }); + + return arc_autorelease(result); +} + -(NSArray*)peopleWithName:(NSString*)name{ __block NSArray *result = nil; rh_dispatch_sync_for_addressbook(self, ^{ @@ -778,6 +833,45 @@ -(NSArray*)peopleForABRecordRefs:(CFArrayRef)peopleRefs{ return [NSArray arrayWithArray:people]; } +/** + * + * + * @param peopleRefs + * @see http://stackoverflow.com/a/11480352/255463 + * @see https://github.com/RigilCorp/ABManager/blob/4abd69d28aef5fa11c6458b694cdc187f469364d/ABManagerExample/ABManager.m#L62 + * + * @return NSArray + */ +- (NSArray *)peopleUnifiedForABRecordRefs:(CFArrayRef)peopleRefs +{ + if (!peopleRefs) return nil; + + NSMutableSet *unifiedRecordsSet = [NSMutableSet set]; + + rh_dispatch_sync_for_addressbook(self, ^{ + NSMutableSet *linkedPersonsToSkip = [NSMutableSet set]; + + for (CFIndex i = 0; i < CFArrayGetCount(peopleRefs); i++) { + ABRecordRef record = CFArrayGetValueAtIndex(peopleRefs, i); + + if ([linkedPersonsToSkip containsObject:(__bridge id) record]) { + continue; + } + + NSArray *linkedRecordsArray = (__bridge NSArray *) ABPersonCopyArrayOfAllLinkedPeople(record); + + if (linkedRecordsArray.count > 1) { + [linkedPersonsToSkip addObjectsFromArray:linkedRecordsArray]; + } + + RHPerson *person = [self personForABRecordRef:record]; + if (person) [unifiedRecordsSet addObject:person]; + } + }); + + return unifiedRecordsSet.allObjects; +} + -(RHPerson*)personForABRecordID:(ABRecordID)personID{ __block ABRecordRef recordRef = NULL;