0

私のアプリABPropertyIDではデータベースに保存し、それに基づいての数を表示しましたがABRecordRefABAddressBookGetPersonWithRecordID(addressBook, recordID).連絡先で連絡先を削除して取得しようとするとABRecordRef、アプリがクラッシュするのは明らかです。

だから私の質問は:

recordIDがaddressBookで使用可能であることを確認する方法はありますか?

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 に答える