0

在我的应用程序中,我已存储ABPropertyID在数据库中,并基于此显示了数量,ABRecordRef但是ABAddressBookGetPersonWithRecordID(addressBook, recordID).当在联系人中删除联系人并尝试获取ABRecordRef时,应用程序崩溃是显而易见的。

所以我的问题是:

有什么方法可以检查地址簿中的记录 ID 是否可用?

4

2 回答 2

1

此函数不会抛出异常,但如果没有具有此 ID 的记录,则返回 NULL。

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

于 2012-12-18T07:14:29.810 回答
0

试试这个

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
 }        }
于 2012-12-18T07:26:51.937 回答