Creating a Contacts application — What can I do to handle email/phone numbers better?

谁都会走 提交于 2019-12-12 01:44:30

问题


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

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;
    }

}

回答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.



来源:https://stackoverflow.com/questions/20937083/creating-a-contacts-application-what-can-i-do-to-handle-email-phone-numbers-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!