0

My application ties into the Addressbook framework for iOS, and I'm grabbing contact information.

  • firstname
  • lastname
  • company name
  • home email
  • work email
  • home phone
  • work phone
  • cell phone

I need to expand on what information I'm grabbing. The issue I'm having in the app, is that it imports the contacts into the users database on the app. When this happens, if I have more than one home email(or any other email/phone field) then it imports two users.

Here is my Person model

#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>

@interface Person : NSObject

@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *fullName;
@property (nonatomic, strong) NSString *homeEmail;
@property (nonatomic, strong) NSString *workEmail;


@property (nonatomic, strong) NSString *companyName;
@property (nonatomic, strong) NSString *homePhone;
@property (nonatomic, strong) NSString *workPhone;
@property (nonatomic, strong) NSString *cellPhone;

@property (nonatomic, strong) NSString *sortByName; // Used only for sorting



@property (nonatomic) ABRecordID publicContactRecordId;
@property (nonatomic) NSString *parseObjectId;

- (NSComparisonResult)compareFirstName:(Person *)otherObject;

@end

I guess maybe I should have them as a Dictionary or an Array? The issue is right now, obviously I can't have more than one home email and any other field.

This may provide some more insight, I'm saving Parse User Database To Store Device Contacts To

EDIT:

Here is how I'm retrieving the email and phone numbers from the address book api.

//email
ABMultiValueRef emails = ABRecordCopyValue(contactPerson,
                                           kABPersonEmailProperty);
NSUInteger j = 0;
for (j = 0; j < ABMultiValueGetCount(emails); j++){
    NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
    if (j == 0){
        if(email){
            person.homeEmail = email;
        }
    }
    else if (j==1){
        if(email){
            person.workEmail = email;
        }
    }
}

ABMultiValueRef numbers = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);

for (CFIndex i = 0; i < ABMultiValueGetCount(numbers); i++) {
    NSString *phoneLabel = (__bridge NSString *)ABMultiValueCopyLabelAtIndex(numbers, i);
    NSString *phoneNumber = (__bridge NSString *)ABMultiValueCopyValueAtIndex(numbers, i);
    //NSLog(@"%@ | %@",phoneLabel,phoneNumber);
    if([phoneLabel isEqualToString:@"Home Phone"])
    {
        person.homePhone = phoneNumber;
    }else if([phoneLabel isEqualToString:@"Work Phone"])
    {
        person.workPhone = phoneNumber;
    }else if([phoneLabel isEqualToString:@"Cell Phone"])
    {
        person.cellPhone = phoneNumber;
    }

}
Envin
  • 1,463
  • 8
  • 32
  • 69
  • This isn't really the answer to your question, but it might be helpful. An Objective-C wrapper for the address book. https://github.com/erica/ABContactHelper – Kevin Jan 05 '14 at 18:31

1 Answers1

1

There are two ways of structuring this:

  1. Modify your Person class to have a email property that's a NSArray of NSDictionary entries—each dictionary will have an address and a type (home, work, etc). A similar approach can be taken for phone.

  2. Modify your Person class so that homeEmail, workEmail, homePhone, and workPhone are NSArray types and each holds an array of strings representing the email addresses and phone numbers.

For both options, you'll need two extra tables in the database—one for email addresses and one for phone numbers. Their structure would be pretty basic: userid (from the users table), type (home, work, etc), and a email address or phone number column.

neilco
  • 7,964
  • 2
  • 36
  • 41
  • I like this answer, and I want to start prototyping it. Can you provide some insight on if my method of retrieving email addresses and phone numbers is the proper way for your first choice? I edited my main post. – Envin Jan 05 '14 at 19:15
  • @envinyater The loops are the correct way, yes. You will need to extract & parse the type of email address/phone number too. You can do this with `ABMultiValueCopyLabelAtIndex`. It returns a `CFStringRef` you can use toll-free bridging to cast this to a `NSString`. It will look something like `_$!!$_`. – neilco Jan 05 '14 at 20:04