Skip to content

implements unified people and adds pod spec file #77

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions RHAddressBook.podspec
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions RHAddressBook/RHAddressBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
94 changes: 94 additions & 0 deletions RHAddressBook/RHAddressBook.m
Original file line number Diff line number Diff line change
Expand Up @@ -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, ^{
Expand Down Expand Up @@ -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;
Expand Down