0

In my app I've stored ABPropertyID in database and based on that I've displayed number of ABRecordRef with ABAddressBookGetPersonWithRecordID(addressBook, recordID). But when contact deleted in contacts and I try to get ABRecordRef, App crashes is obvious.

So my question is:

Is there any way to check that recordID is available in addressBook?

RivieraKid
  • 5,923
  • 4
  • 38
  • 47
iBhavik
  • 663
  • 11
  • 28

2 Answers2

1

This function does not throw an exception, but it returns NULL if there is no record with this ID.

See: http://developer.apple.com/library/ios/ipad/#documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html

Cocoanetics
  • 8,171
  • 2
  • 30
  • 57
0

try this one

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
        // First time access has been granted, add the contact
        ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);

        ABRecordRef newPerson = ABPersonCreate();

        ABRecordSetValue(newPerson, kABPersonSortByFirstName,(__bridge CFTypeRef)(firstname),&error);
        ABRecordSetValue(newPerson, kABPersonLastNameProperty,(__bridge CFTypeRef)(lastname), &error);
        ABRecordSetValue(newPerson, kABPersonOrganizationProperty, @"", &error);
        ABRecordSetValue(newPerson, kABPersonJobTitleProperty,@"", &error);



        ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(contact_phone), kABPersonPhoneMainLabel, NULL);
        ABMultiValueAddValueAndLabel(multiPhone, @"", kABPersonPhoneMobileLabel, NULL);
        ABMultiValueAddValueAndLabel(multiPhone, @"", kABOtherLabel, NULL);
        ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
        CFRelease(multiPhone);



        ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(multiEmail,(__bridge CFTypeRef)(contact_email) , kABWorkLabel, NULL);
        ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);
        CFRelease(multiEmail);

        //        ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
        //    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
        //    [addressDictionary setObject:@"750 North Orleans Street, Ste 601" forKey:(NSString *) kABPersonAddressStreetKey];
        //    [addressDictionary setObject:@"Chicago" forKey:(NSString *)kABPersonAddressCityKey];
        //    [addressDictionary setObject:@"IL" forKey:(NSString *)kABPersonAddressStateKey];
        //    [addressDictionary setObject:@"60654" forKey:(NSString *)kABPersonAddressZIPKey];
        //    ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
        //    ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress,&error);
        //    CFRelease(multiAddress);

        ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
        ABAddressBookSave(iPhoneAddressBook, &error);

        if (error != NULL)
        {
            NSLog(@"Danger Will Robinson! Danger!");
        }


        // ABAddressBookRef addressBook = ABAddressBookCreate();


    });
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    CFErrorRef error = NULL;
   // ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, NULL);

    ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);

    ABRecordRef newPerson = ABPersonCreate();

    ABRecordSetValue(newPerson, kABPersonSortByFirstName,(__bridge CFTypeRef)(firstname),&error);
    NSLog(@"%@",(__bridge CFTypeRef)(firstname));
    ABRecordSetValue(newPerson, kABPersonLastNameProperty,(__bridge CFTypeRef)(lastname), &error);
    NSLog(@"%@",(__bridge CFTypeRef)(lastname));

    ABRecordSetValue(newPerson, kABPersonOrganizationProperty, @"", &error);
    ABRecordSetValue(newPerson, kABPersonJobTitleProperty,@"", &error);

    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone,(__bridge CFTypeRef)(contact_phone), kABPersonPhoneMainLabel, NULL);
    ABMultiValueAddValueAndLabel(multiPhone, @"", kABPersonPhoneMobileLabel, NULL);
    ABMultiValueAddValueAndLabel(multiPhone, @"", kABOtherLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
    CFRelease(multiPhone);



    ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiEmail, (__bridge CFTypeRef)(contact_email), kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);
    CFRelease(multiEmail);

    //        ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    //    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
    //    [addressDictionary setObject:@"750 North Orleans Street, Ste 601" forKey:(NSString *) kABPersonAddressStreetKey];
    //    [addressDictionary setObject:@"Chicago" forKey:(NSString *)kABPersonAddressCityKey];
    //    [addressDictionary setObject:@"IL" forKey:(NSString *)kABPersonAddressStateKey];
    //    [addressDictionary setObject:@"60654" forKey:(NSString *)kABPersonAddressZIPKey];
    //    ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
    //    ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress,&error);
    //    CFRelease(multiAddress);



    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
    ABAddressBookSave(iPhoneAddressBook, &error);
    alertvalue++;

    if (error != NULL)
    {
        NSLog(@"Error not found!");
    }


    // ABAddressBookRef addressBook = ABAddressBookCreate();


}
 else {
    // The user has previously denied access
    // Send an alert telling user to change privacy setting in settings app
 }        }
  • when you get details from ,first u have to check whther is it null or not ? then add –  Dec 18 '12 at 07:31