Skip to content

Instantly share code, notes, and snippets.

@bbuck
Created May 15, 2014 20:10
Show Gist options
  • Save bbuck/1811c9b17eeac8a70463 to your computer and use it in GitHub Desktop.
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.
#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
#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