Multi value url addressbook iphone

Multi value url addressbook iphone

I want to create a new contact in the addressbook.
The problem appears when you want to store more URLs (web addresses to some social networks).
My code works perfectly in simulator of iOS6. But in real iPhone with iOS6, stores all values ​​except the urls.
I've been looking for a few days and can not find a solution, I will be very grateful if anyone can help.
My code:
-(void) addContactToAddressBook:(ABAddressBookRef) iPhoneAddressBook 
{

    CFErrorRef error = NULL;

    ABRecordRef newPerson = ABPersonCreate();

    //Name and phone number
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef)_nameField.text, &error);
    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFStringRef)_phoneField.text, kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);

    //Email value
    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef)_emailField.text, kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
    CFRelease(emailMultiValue);

    //URL values
    ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_FacebookField.text, (CFStringRef)@"Facebook", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_twitterField.text, (CFStringRef)@"Twitter", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_linkedinField.text, (CFStringRef)@"Linkedin", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_googleField.text, (CFStringRef)@"Google+", NULL);
    ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, nil);
    CFRelease(urlMultiValue);

    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);

    ABAddressBookSave(iPhoneAddressBook, &error);
    if (error != NULL)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Contact not saved" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact saved" message:@"Your contact was successfully saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}

Thanks!

Solutions/Answers:

Answer 1:

As per the documentation on apple’s site (scroll down to Privacy in the middle of the page), access to the address book must be granted before it can be access programmatically. Here is what I ended up doing.

-(void)requestPermission
{
        ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);

        if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
            ABAddressBookRequestAccessWithCompletion(iPhoneAddressBook, ^(bool granted, CFErrorRef error) {
                if (granted) {
                    // First time access has been granted, add the contact
                    [self addContactToAddressBook:iPhoneAddressBook];
                } else {
                    // User denied access
                    // Display an alert telling user the contact could not be added
                }
            });
        }
        else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
            // The user has previously given access, add the contact
            [self addContactToAddressBook:iPhoneAddressBook];
        }
        else {
            // The user has previously denied access
            // Send an alert telling user to change privacy setting in settings app
        }

    }

-(void)addContactToAddressBook:(ABAddressBookRef)iPhoneAddressBook
{
    CFErrorRef error = NULL;
    ABRecordRef newPerson = ABPersonCreate();

    //Name and phone number
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef)_nameField.text, &error);
    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFStringRef)_phoneField.text, kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
    CFRelease(multiPhone);

    //Email value
    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef)_emailField.text, kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
    CFRelease(emailMultiValue);

    //URL values
    ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_FacebookField.text, (CFStringRef)@"Facebook", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_twitterField.text, (CFStringRef)@"Twitter", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_linkedinField.text, (CFStringRef)@"Linkedin", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_googleField.text, (CFStringRef)@"Google+", NULL);
    ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, nil);
    CFRelease(urlMultiValue);

    // ...
    // Set other properties
    // ...
    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);

    ABAddressBookSave(iPhoneAddressBook, &error);
    CFRelease(newPerson);
    CFRelease(iPhoneAddressBook);
    if (error != NULL)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Contact not saved" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact saved" message:@"Your contact was successfully saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}

References

Loading...