Ok, so I got a contact to go to my phone, and it works fine, except for two things. Firstly, it does not work for the first time, because it asks me for access to contacts. How can I do this so that he adds it after the user gives access to contacts? Secondly, is there a way to open a contact so that the user can view it after it is created? This is what I did:
- (IBAction)addContact:(id)sender {
ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
ABAddressBookRef addressBook = [peoplePicker addressBook];
ABRecordRef person = ABPersonCreate();
NSString *organization = @"American Business Center";
NSString *personEmail = @"leasing@americanbusinesscenter.com";
NSString *phoneNo = @"(727)536-6379";
NSString *webURL = @"www.AmericanBusinessCenter.com";
NSString *addressOne = @"8340 Ulmerton Road";
NSString *addressTwo = @"Suite 202";
NSString *cityName =@ "Largo";
NSString *stateName = @"Florida";
NSString *pinCode = @"34209";
NSString *country = @"United States";
CFErrorRef cfError=nil;
ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);
if (webURL)
{
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
CFRelease(urlMultiValue);
}
if (personEmail)
{
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);
}
if (phoneNo)
{
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
for (NSString *venuePhoneNumberString in venuePhoneNumbers)
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
CFRelease(phoneNumberMultiValue);
}
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
if (addressOne)
{
if (addressTwo)
addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@\n%@", addressOne, addressTwo];
else
addressDictionary[(NSString *) kABPersonAddressStreetKey] = addressOne;
}
if (cityName)
addressDictionary[(NSString *)kABPersonAddressCityKey] = cityName;
if (stateName)
addressDictionary[(NSString *)kABPersonAddressStateKey] = stateName;
if (pinCode)
addressDictionary[(NSString *)kABPersonAddressZIPKey] = pinCode;
if (country)
addressDictionary[(NSString *)kABPersonAddressCountryKey] = country;
ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
CFRelease(multiAddress);
ABAddressBookAddRecord(addressBook, person, &cfError);
if (ABAddressBookSave(addressBook, nil)) {
NSLog(@"\nPerson Saved successfuly");
} else {
NSLog(@"\n Error Saving person to AddressBook");
}
}
Edit
Based on the LML answer, I did the following:
- (IBAction)addContact:(id)sender {
ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
ABAddressBookRef addressBook = [peoplePicker addressBook];
ABRecordRef person = ABPersonCreate();
NSString *organization = @"American Business Center";
NSString *personEmail = @"leasing@americanbusinesscenter.com";
NSString *phoneNo = @"(727)536-6379";
NSString *webURL = @"www.AmericanBusinessCenter.com";
NSString *addressOne = @"8340 Ulmerton Road";
NSString *addressTwo = @"Suite 202";
NSString *cityName =@ "Largo";
NSString *stateName = @"Florida";
NSString *pinCode = @"34209";
NSString *country = @"United States";
CFErrorRef cfError=nil;
ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);
if (webURL)
{
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
CFRelease(urlMultiValue);
}
if (personEmail)
{
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);
}
if (phoneNo)
{
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
for (NSString *venuePhoneNumberString in venuePhoneNumbers)
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
CFRelease(phoneNumberMultiValue);
}
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
if (addressOne)
{
if (addressTwo)
addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@\n%@", addressOne, addressTwo];
else
addressDictionary[(NSString *) kABPersonAddressStreetKey] = addressOne;
}
if (cityName)
addressDictionary[(NSString *)kABPersonAddressCityKey] = cityName;
if (stateName)
addressDictionary[(NSString *)kABPersonAddressStateKey] = stateName;
if (pinCode)
addressDictionary[(NSString *)kABPersonAddressZIPKey] = pinCode;
if (country)
addressDictionary[(NSString *)kABPersonAddressCountryKey] = country;
ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
CFRelease(multiAddress);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if(error == NULL){
NSLog(@"Success");
ABAddressBookAddRecord(addressBook, person, &cfError);
}
});
CFRelease(addressBook);
CFRelease(person);
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
ABAddressBookSave(addressBook, &cfError);
if(cfError == NULL){
NSLog(@"Success") ;
}
CFRelease(addressBook);
CFRelease(person);
}
else {
NSLog(@"Adding to address book failed");
}
}
Now it crashes when I click the button and I get an error message:
'const CFErrorRef *' (aka 'struct __CFError * const *') 'CFErrorRef *' (aka 'struct __CFError **')
, : P