Advertisement
Guest User

HALContact.m

a guest
Jul 8th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "HALContact.h"
  2.  
  3. @interface HALContact ()
  4.  
  5. // Redeclare readonly properties
  6. @property (readwrite) NSMutableArray *phoneNumbers;
  7. @property (readwrite) NSString *firstName;
  8.  
  9. @end
  10.  
  11. @implementation HALContact
  12.  
  13. #pragma mark - Accessor Methods
  14.  
  15. - (NSString *)firstName
  16. {
  17.    
  18.     if (!_firstName) {
  19.         _firstName = [[NSString alloc]init];
  20.     }
  21.         _firstName = (__bridge_transfer NSString
  22.                           *)ABRecordCopyValue(_contactRef, kABPersonFirstNameProperty);
  23.    
  24.     return _firstName;
  25. }
  26.  
  27. - (NSArray *)phoneNumbers {
  28.    
  29.     //Remove any phone numbers from previous contact.
  30.     if (_phoneNumbers) {
  31.         [_phoneNumbers removeAllObjects];
  32.     }
  33.    
  34.     if (!_phoneNumbers) {
  35.         //Create fresh _phoneNumbers array
  36.         _phoneNumbers = [[NSMutableArray alloc]init];
  37.     }
  38.    
  39.    
  40.     ABMultiValueRef *phoneNumberRef = ABRecordCopyValue(_contactRef, kABPersonPhoneProperty);
  41.    
  42.     NSString *phoneNumber = [[NSString alloc] init];
  43.    
  44.     // Make sure multi value ref exists
  45.     if (phoneNumberRef) {
  46.         CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumberRef);
  47.        
  48.         for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
  49.             phoneNumber = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneNumberRef, i);
  50.             CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumberRef, i);
  51.             if (label) {
  52.                
  53.             [_phoneNumbers addObject:phoneNumber];
  54.                 NSLog(@"phoneNumbers count in method: %d", _phoneNumbers.count);
  55.             }
  56.             CFRelease(label);
  57.         }
  58.         CFRelease(phoneNumberRef);
  59.     }
  60.    
  61.     return _phoneNumbers;
  62.  
  63. }
  64.  
  65. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement