Created
May 15, 2014 20:10
-
-
Save bbuck/1811c9b17eeac8a70463 to your computer and use it in GitHub Desktop.
A small wrapper for ABRecordRef values to take the logic out of doing some of the data fetches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ABRecordRefWrapper.h" | |
@interface ABRecordRefWrapper () | |
@property (nonatomic) ABRecordRef person; | |
@property (strong, nonatomic) NSArray *numbersCache; | |
@property (strong, nonatomic) NSString *firstNameCache; | |
@property (strong, nonatomic) NSString *lastNameCache; | |
@end | |
@implementation ABRecordRefWrapper | |
- (instancetype)initWithABRecordRef:(ABRecordRef)person { | |
self = [super init]; | |
if (self) { | |
self.person = person; | |
} | |
return self; | |
} | |
- (NSString *)firstName { | |
if (!self.firstNameCache && self.person) { | |
self.firstNameCache = (__bridge NSString *)ABRecordCopyValue(self.person, kABPersonFirstNameProperty); | |
} | |
return self.firstNameCache; | |
} | |
- (NSString *)lastName { | |
if (!self.lastNameCache && self.person) { | |
self.lastNameCache = (__bridge NSString *)ABRecordCopyValue(self.person, kABPersonLastNameProperty); | |
} | |
return self.lastNameCache; | |
} | |
- (NSString *)fullNameWithOptions:(ABPersonCompositeNameFormat)nameFormat { | |
if (!self.person) { | |
return nil; | |
} | |
NSString *fullName; | |
switch (nameFormat) { | |
case kABPersonCompositeNameFormatFirstNameFirst: { | |
if ([self firstName]) { | |
fullName = [NSString stringWithFormat:@"%@", [self firstName]]; | |
if ([self lastName]) { | |
fullName = [fullName stringByAppendingFormat:@" %@", [self lastName]]; | |
} | |
} else if ([self lastName]) { | |
fullName = [NSString stringWithFormat:@"%@", [self lastName]]; | |
} | |
break; | |
} | |
case kABPersonCompositeNameFormatLastNameFirst: { | |
if ([self lastName]) { | |
fullName = [NSString stringWithFormat:@"%@", [self lastName]]; | |
if ([self firstName]) { | |
fullName = [fullName stringByAppendingFormat:@", %@", [self firstName]]; | |
} | |
} else if ([self firstName]) { | |
fullName = [NSString stringWithFormat:@"%@", [self firstName]]; | |
} | |
break; | |
} | |
} | |
return fullName; | |
} | |
- (NSArray *)phoneNumbers { | |
if (!self.numbersCache && self.person) { | |
NSMutableArray *phones = [[NSMutableArray alloc] init]; | |
ABMultiValueRef phoneMulti = ABRecordCopyValue(self.person, kABPersonPhoneProperty); | |
for (NSInteger i = 0, end = ABMultiValueGetCount(phoneMulti); i < end; ++i) { | |
NSString *phoneNumber = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMulti, i); | |
[phones addObject:phoneNumber]; | |
} | |
self.numbersCache = [[NSArray alloc] initWithArray:phones]; | |
} | |
return self.numbersCache; | |
} | |
- (NSInteger)phoneNumberCount { | |
if ([self phoneNumbers]) { | |
return [[self phoneNumbers] count]; | |
} | |
return -1; | |
} | |
- (ABRecordRef)recordReference { | |
return self.person; | |
} | |
- (void)setRecordReference:(ABRecordRef)person { | |
self.person = person; | |
// Clear out cached values | |
self.numbersCache = nil; | |
self.firstNameCache = nil; | |
self.lastNameCache = nil; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <AddressBookUI/AddressBookUI.h> | |
@interface ABRecordRefWrapper : NSObject | |
- (instancetype)initWithABRecordRef:(ABRecordRef)person; | |
- (NSString *)firstName; | |
- (NSString *)lastName; | |
- (NSString *)fullNameWithOptions:(ABPersonCompositeNameFormat)nameFormat; | |
- (NSArray *)phoneNumbers; | |
- (NSInteger)phoneNumberCount; | |
- (ABRecordRef)recordReference; | |
- (void)setRecordReference:(ABRecordRef)person; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment