下面的代码在模拟器(iOS 4 和 5)中运行良好,但在 iOS 4 设备上没有添加任何联系人。不返回错误。你知道为什么吗?
ABAddressBookRef ab = ABAddressBookCreate();
abGroupRef = ABAddressBookGetGroupWithRecordID(ab, 1);
ABRecordRef pRef = ABAddressBookGetPersonWithRecordID(ab, 1);
ABGroupAddMember(abGroupRef, pRef, &anError);
ABAddressBookSave(ab, &anError);
谢谢,
--约瑟夫
Best Answer-推荐答案 strong>
Objective C 编程习惯总是检查返回 BOOL 值的函数的结果(例如 ABGroupAddMember 或 ABAddressBookSave 或 NSArray 或 NSDictionary writeToFile 方法)before 检查是否有错误作为参数之一返回.
试试这样的:
ABAddressBookRef ab = ABAddressBookCreate();
ABRecordRef abGroupRef = ABAddressBookGetGroupWithRecordID(ab, 1);
ABRecordRef pRef = ABAddressBookGetPersonWithRecordID(ab, 1);
CFErrorRef anError = NULL;
NSError * anErrorAsObjectiveCObject; // for use with toll free bridged object magic
if(ABGroupAddMember(abGroupRef, pRef, &anError))
{
if(ABAddressBookSave(ab, &anError))
{
NSLog(@"successfully saved");
} else {
anErrorAsObjectiveCObject = (__bridge_transfer NSError *) anError;
NSLog( @"error in ABAddressBookSave - %@ %@", [anErrorAsObjectiveCObject domain], [anErrorAsObjectiveCObject localizedDescription] );
}
} else {
anErrorAsObjectiveCObject = (__bridge_transfer NSError *) anError;
NSLog( @"error in ABGroupAddMember - %@ %@", [anErrorAsObjectiveCObject domain], [anErrorAsObjectiveCObject localizedDescription] );
}
关于iphone - ABGroupAddMember 在 iOS4 设备上不起作用,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/8525360/
|