私はこのコードを持っています:
void MyAddressBookExternalChangeCallback(ABAddressBookRef addressBook, CFDictionaryRef info, void *context) {
[(id)context reloadSpecifiers];
}
@interface JBPPreferencesListController : PSListController {
ABAddressBookRef addressBook;
}
@end
@implementation JBPPreferencesListController
-(id)init {
if (self = [super init]) {
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
// ABAddressBookRegisterExternalChangeCallback(addressBook, MyAddressBookExternalChangeCallback, self);
}
return self;
}
- (id)specifiers {
if (_specifiers == nil) {
NSMutableArray *specs = [NSMutableArray array];
[specs addObjectsFromArray:[self specifiersForContactGroups]];
_specifiers = [specs copy];
}
return _specifiers;
}
-(NSArray *)specifiersForContactGroups {
if (addressBook == NULL) {
return nil;
}
CFIndex groupCount = ABAddressBookGetGroupCount(addressBook);
if (groupCount == 0) {
return nil;
}
CFArrayRef allGroups = ABAddressBookCopyArrayOfAllGroups(addressBook);
if (allGroups == NULL) {
return nil;
}
PSSpecifier* allContactsSpec = [PSSpecifier preferenceSpecifierListItemNamed:[NSString stringWithFormat:@"Favourites %i", groupCount] value:@(-2)];
NSMutableArray *groupSpecs = [NSMutableArray arrayWithObject:allContactsSpec];
for (int i = 0; i < groupCount; i++) {
ABRecordRef currentGroup = CFArrayGetValueAtIndex(allGroups, i);
if (currentGroup != NULL) {
NSString *groupName = (NSString *)ABRecordCopyValue(currentGroup, kABGroupNameProperty);
NSNumber *groupID = @(ABRecordGetRecordID(currentGroup));
PSSpecifier* specifier = [PSSpecifier preferenceSpecifierListItemNamed:groupName value:groupID];
[groupName release];
[groupSpecs addObject:specifier];
}
}
CFRelease(allGroups);
return [[groupSpecs copy] autorelease];
}
-(void)dealloc {
if (addressBook != NULL) CFRelease(addressBook);
[super dealloc];
}
@end
ABAddressBookRegisterExternalChangeCallback
の行のコメントinit
を外すと、(コントローラーのビューが画面に表示される前に) EXC_BAD_ACCESS とセグメンテーション違反 11 でクラッシュします。
私は ARC を使用しておらず、電話にグループがあります (行がコメントアウトされている場合に表示されます)。