ABGroupCreate无法使用Exchange

时间:2012-02-09 09:07:23

标签: ios iphone addressbook

当我在它运行的模拟器上运行这段代码时遇到问题,我得到了组的id但是当我在设备上运行时,id被设置为-1 ...但是错误消息保持无效。

    -(NSNumber *)addGroupeToAddressbookWithName:(NSString *)name{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFErrorRef error = NULL;
    ABRecordRef group = ABGroupCreate();
    ABRecordSetValue(group, kABGroupNameProperty,(__bridge CFStringRef)name, &error);
    ABAddressBookAddRecord(addressBook, group, &error);
    ABAddressBookSave(addressBook,&error);
    NSNumber *gId = [NSNumber numberWithInt:ABRecordGetRecordID(group)];

    CFRelease(group);
    return gId;
}

我无法弄清楚有什么区别以及如何让它在真实设备上运行。

编辑:如果我在手机上删除了交换同步,但仍然希望它可以在手机上拥有交换帐户时工作,则会发现它有效。所以没有真正解决问题

编辑/回答

发现这是因为交换不了解群组,要保存群组需要使用正确的来源,另请参阅:Obtaining Specific ABSource from ABAddressBook in iOS 4+

新代码:

-(NSNumber *)addGroupeToAddressbookWithName:(NSString *)name{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFErrorRef error = NULL;
    CFArrayRef sources = ABAddressBookCopyArrayOfAllSources(addressBook);
    CFIndex sourceCount = CFArrayGetCount(sources);
    NSNumber *gId = nil;
    for (CFIndex i = 0 ; i < sourceCount; i++) {
        ABRecordRef currentSource = CFArrayGetValueAtIndex(sources, i);
        CFTypeRef sourceType = ABRecordCopyValue(currentSource, kABSourceTypeProperty);

        BOOL isMatch = kABSourceTypeLocal == [(__bridge NSNumber *)sourceType intValue];
        CFRelease(sourceType);

        if (isMatch) {
            ABRecordRef group = ABGroupCreateInSource(currentSource);//ABGroupCreate();
            ABRecordSetValue(group, kABGroupNameProperty,(__bridge CFStringRef)name, &error);
            ABAddressBookAddRecord(addressBook, group, &error);
            ABAddressBookSave(addressBook,&error);
            gId = [NSNumber numberWithInt:ABRecordGetRecordID(group)];
            CFRelease(group);
            CFRelease(currentSource);
            break;
        }
    }

    CFRelease(sources);
    return gId;
}

1 个答案:

答案 0 :(得分:4)

发现这是因为交流不了解群组,要保存群组需要使用正确的来源,请参阅:Obtaining Specific ABSource from ABAddressBook in iOS 4+

新代码:

-(NSNumber *)addGroupeToAddressbookWithName:(NSString *)name{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFErrorRef error = NULL;
    CFArrayRef sources = ABAddressBookCopyArrayOfAllSources(addressBook);
    CFIndex sourceCount = CFArrayGetCount(sources);
    NSNumber *gId = nil;
    for (CFIndex i = 0 ; i < sourceCount; i++) {
        ABRecordRef currentSource = CFArrayGetValueAtIndex(sources, i);
        CFTypeRef sourceType = ABRecordCopyValue(currentSource, kABSourceTypeProperty);

        BOOL isMatch = kABSourceTypeLocal == [(__bridge NSNumber *)sourceType intValue];
        CFRelease(sourceType);

        if (isMatch) {
            ABRecordRef group = ABGroupCreateInSource(currentSource);//ABGroupCreate();
            ABRecordSetValue(group, kABGroupNameProperty,(__bridge CFStringRef)name, &error);
            ABAddressBookAddRecord(addressBook, group, &error);
            ABAddressBookSave(addressBook,&error);
            gId = [NSNumber numberWithInt:ABRecordGetRecordID(group)];
            CFRelease(group);
            CFRelease(currentSource);
            break;
        }
    }

    CFRelease(sources);
    return gId;
}