@blog.justoneplanet.info

日々勉強

iOSのAddressBookを操作する

■グループの検索

CFArrayRef groups = ABAddressBookCopyArrayOfAllGroups(addressBook);
for (int i = 0; i < CFArrayGetCount(groups); i++) {
    ABRecordRef group = CFArrayGetValueAtIndex(groups, i);
    CFStringRef registerdGroupName = ABRecordCopyValue(group, kABGroupNameProperty);
    if (CFStringFind(registerdGroupName, aCFgroupName, kCFCompareCaseInsensitive).location != kCFNotFound) {
        isRegistered = true;
        registerdId = ABRecordGetRecordID(group);
    }
    CFRelease(registerdGroupName);
}
CFRelease(groups);

■グループの作成

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
ABAddressBookRef addressBook = ABAddressBookCreate();
bool b1 = false;
bool b2 = false;
bool b3 = false;
ABRecordRef group = ABGroupCreate();
NSString *groupName = NSLocalizedString(@"APP_NAME", @"グループ名");
CFStringRef aCFgroupName = (__bridge CFStringRef)groupName;
b1 = ABRecordSetValue(group, kABGroupNameProperty, aCFgroupName, nil);
            
if (b1) b2 = ABAddressBookAddRecord(addressBook, group, nil);
if (b2) b3 = ABAddressBookSave(addressBook, nil);
if (b3) {
    [ud setInteger:ABRecordGetRecordID(group) forKey:@"key_ab_group_id"];
    [ud synchronize];
}
CFRelease(group);
CFRelease(addressBook);

■グループの削除

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
ABAddressBookRef addressBook = ABAddressBookCreate();
bool b1 = false;
bool b2 = false;
ABRecordRef group = ABAddressBookGetGroupWithRecordID(addressBook, [ud integerForKey:@"key_ab_group_id"]);
b1 = ABAddressBookRemoveRecord(addressBook, group, nil);
if (b1) b2 = ABAddressBookSave(addressBook, nil);
if (b2) {
    [ud setInteger:0 forKey:@"key_ab_group_id"];
    [ud synchronize];
}
CFRelease(addressBook);

■グループのメンバ取得と削除

CFArrayRef people = ABGroupCopyArrayOfAllMembers(group);
for (int i = 0; i < CFArrayGetCount(people); i++) {
    ABRecordRef person = CFArrayGetValueAtIndex(people, i);
    result1 = ABGroupRemoveMember(group, person, nil);// グループから削除されるだけで電話帳には残る
    result2 = ABAddressBookRemoveRecord(addressBook, person, nil);// 電話帳から削除される
}

■グループへのメンバ追加

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef group = ABAddressBookGetGroupWithRecordID(addressBook, [ud integerForKey:@"key_ab_group_id"]);
ABRecordRef person = ABPersonCreate();

CFStringRef aCFfirst       = (CFStringRef)first;
CFStringRef aCFsecond      = (CFStringRef)second;
CFStringRef aCFfirstYomi   = (CFStringRef)firstYomi;
CFStringRef aCFsecondYomi  = (CFStringRef)secondYomi;
ABRecordSetValue(person, kABPersonFirstNameProperty, aCFfirst, nil);
ABRecordSetValue(person, kABPersonFirstNamePhoneticProperty, aCFfirstYomi, nil);
ABRecordSetValue(person, kABPersonLastNameProperty, aCFsecond, nil);
ABRecordSetValue(person, kABPersonLastNamePhoneticProperty, aCFsecondYomi, nil);

ABAddressBookAddRecord(addressBook, person, &error);
ABAddressBookSave(addressBook, &error);

if ([ud integerForKey:@"key_ab_group_id"] != 0) {
    ABGroupAddMember(group, person, &error);
}

ABAddressBookSave(addressBook, nil);
CFRelease(person);
CFRelease(addressBook);

参考

コメントはまだありません»

No comments yet.

RSS feed for comments on this post.TrackBack URL

Leave a comment