페이지

2012년 6월 16일 토요일

error when calls the ABAddressBookAddRecord..


// for debugging (thank you, simon)
- (void) loadGroups
{
    // Fetch the address book 
    ABAddressBookRef address = ABAddressBookCreate();
    
    // number of groups?
    CFIndex count = ABAddressBookGetGroupCount(address);
    NSLog(@"Number of groups:%ld", count);
    
    if ( count > 0 ) {
        // Copy: transfer ownership to ARC
        NSArray *groups = (__bridge_transfer  NSArray *) ABAddressBookCopyArrayOfAllGroups(address);
        for( id obj in groups ) {
            ABRecordRef group = (__bridge ABRecordRef) obj;
            // Copy: transfer ownership to ARC
            NSString *groupName = (__bridge_transfer NSString *) ABRecordCopyValue( group, kABGroupNameProperty);
            NSLog(@"Group name:%@", groupName);
            ABRecordID recordId = ABRecordGetRecordID(group);
            NSLog(@"Group record id:%d", recordId); 
            
        }
    } else {
        // Add one sample group
        ABRecordRef grouprec = ABGroupCreate();
        CFErrorRef error;
        BOOL success = ABRecordSetValue(grouprec, kABGroupNameProperty, (CFStringRef) @"My New Group", &error);
        if (!success) NSLog(@"Error: %@", [(__bridge NSError *)error localizedDescription]);        
        
        // Copy: transfer ownership to ARC
        NSString *groupName = (__bridge_transfer NSString *) ABRecordCopyValue(grouprec, kABGroupNameProperty);
        NSLog(@"Group name:%@", groupName);
        
        ABRecordID recordId = ABRecordGetRecordID(grouprec);
        NSLog(@"Group record id:%d", recordId); 
        
        if ( ABAddressBookAddRecord(addressBook, grouprec, &error) ) {
            if ( !ABAddressBookSave(addressBook, &error) ) {
                NSLog(@"Error saving address book:%@", [(__bridge NSError *) error description]);
            }
        } else {
            NSLog(@"Error adding record:%@", [(__bridge NSError *) error description]);
        }
        
    }
}

댓글 없음:

댓글 쓰기