I have a simple form that is only taking four fields right now (I'll add in more later)

  • First Name
  • Last Name
  • Home Email
  • Work Email

I only want to save items that have a value as a new contact. Right now, if I don't enter in a value, then in the contact page the value is "NULL". What is the appropriate way to handle this?

Here is my code

#pragma mark - Add New Contacts Methods

- (IBAction)savePublicContact:(UIBarButtonItem *)sender {        
    CFErrorRef  anError = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &anError);
    ABRecordRef person = ABPersonCreate();
    Person *personUserDefined = [self populatePersonToSave];

    ABRecordSetValue(person,kABPersonFirstNameProperty,(__bridge CFTypeRef)(personUserDefined.firstName),&anError);
    ABRecordSetValue(person,kABPersonLastNameProperty,(__bridge CFTypeRef)(personUserDefined.lastName),&anError);

    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.homeEmail), (CFStringRef)@"Home Email", NULL);
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.workEmail), (CFStringRef)@"Work Email", NULL);
    ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, &anError);

    ABAddressBookAddRecord(addressBook, person, &anError);

    if(ABAddressBookSave(addressBook, &anError)){
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Added Contact"
                                                               message:@"You successfully added a contact"
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];

    }else{
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Contact Error"
                                                               message:(@"Contact was not able to be added")
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
    }


}

- (Person *)populatePersonToSave
{
    Person *person = [[Person alloc] init];

    if([self.firstNameToSaveTextField.text length] > 0){
        person.firstName = self.firstNameToSaveTextField.text;
    }

    if([self.lastNameToSaveTextField.text length] > 0) {
        person.lastName = self.lastNameToSaveTextField.text;
    }

    if([self.workEmailToSaveTextField.text length] > 0){
        person.workEmail = self.workEmailToSaveTextField.text;
    }

    if([self.homeEmailToSaveTextField.text length] > 0){
        person.homeEmail = self.homeEmailToSaveTextField.text;
    }

    return person;
}
有帮助吗?

解决方案

Better you put the check for null condition where you are adding contact to address-book, its because at every time you are initiating the required field in contact list .if you have no data to input in required field why should we initiate it or refer it.

Better you use the modified code:

if (personUserDefined.firstName) {
    ABRecordSetValue(person,kABPersonFirstNameProperty,(__bridge CFTypeRef)(personUserDefined.firstName),&anError);
}


if (personUserDefined.lastName) {
    ABRecordSetValue(person,kABPersonLastNameProperty,(__bridge CFTypeRef)(personUserDefined.lastName),&anError);
}

ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
if (personUserDefined.homeEmail) {
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.homeEmail), (CFStringRef)@"Home Email", NULL);
}

if (personUserDefined.workEmail) {
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.workEmail), (CFStringRef)@"Work Email", NULL);
}
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, &anError);
ABAddressBookAddRecord(addressBook, person, &anError);

above code provide you how to prevent inserting null values in addressbook. please check if condition satisfying conditions (i had not executed on system).

其他提示

#pragma mark - Add New Contacts Methods

- (IBAction)savePublicContact:(UIBarButtonItem *)sender {

    CFErrorRef  anError = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &anError);
    ABRecordRef person = ABPersonCreate();

    if([self isvalidDetails]){ //New method to validate details - this will return yes/no based on validation
        Person *personUserDefined = [self populatePersonToSave];
        ABRecordSetValue(person,kABPersonFirstNameProperty,(__bridge CFTypeRef)(personUserDefined.firstName),&anError);
        ABRecordSetValue(person,kABPersonLastNameProperty,(__bridge CFTypeRef)(personUserDefined.lastName),&anError);

        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.homeEmail), (CFStringRef)@"Home Email", NULL);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(personUserDefined.workEmail), (CFStringRef)@"Work Email", NULL);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, &anError);

        ABAddressBookAddRecord(addressBook, person, &anError);

        if(ABAddressBookSave(addressBook, &anError)){
            UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Added Contact"
                                                                   message:@"You successfully added a contact"
                                                                  delegate:self
                                                         cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertsuccess show];

        }else{
            UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Contact Error"
                                                                   message:(@"Contact was not able to be added")
                                                                  delegate:self
                                                         cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertsuccess show];
        }
    }

}

-(BOOL)isValidDetails{
    BOOL isValidated = TRUE;

    if([self.firstNameToSaveTextField.text length] <= 0){ //Trim and check length
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Required field"
                                                               message:(@"Please enter firstname")
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
        isValidated = FALSE;
        return isValidated;
    }

    if([self.lastNameToSaveTextField.text length] > 0) {
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Required field"
                                                               message:(@"Please enter lastname")
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
        isValidated = FALSE;
        return isValidated;
    }

    if([self.workEmailToSaveTextField.text length] > 0){
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Required field"
                                                               message:(@"Please enter work email")
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
        isValidated = FALSE;
        return isValidated;
    }

    if([self.homeEmailToSaveTextField.text length] > 0){
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Required field"
                                                               message:(@"Please enter home email")
                                                              delegate:self
                                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
        isValidated = FALSE;
        return isValidated;
    }
    return isValidated;
}

- (Person *)populatePersonToSave
{
    Person *person = [[Person alloc] init];

    if([self.firstNameToSaveTextField.text length] > 0){
        person.firstName = self.firstNameToSaveTextField.text;
    }

    if([self.lastNameToSaveTextField.text length] > 0) {
        person.lastName = self.lastNameToSaveTextField.text;
    }

    if([self.workEmailToSaveTextField.text length] > 0){
        person.workEmail = self.workEmailToSaveTextField.text;
    }

    if([self.homeEmailToSaveTextField.text length] > 0){
        person.homeEmail = self.homeEmailToSaveTextField.text;
    }

    return person;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top