iPhone: Accessing Composite Name in AddressBook Causes EXC_BAD_ACCESS
I'm new to iPhone development and have a question I hope someone can help me with.
I have a programmer working on an iPhone app for me and when I run the app in the simulator, it works great. But when I try to run it on my actual iPhone, I get a EXC_BAD_ACCESS error and the app locks up.
Looking at the debugger, it's referencing the following code in my MainController as the problem:
-(void)loadAddressBook{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
addressBookLoaded=1;
[AddressbookRecord readAllContactTable:[self getDBPath]];
ABAddressBookRef addressBook = ABAddressBookCreate();
for(NSUInteger i=1;i<=ABAddressBookGetPersonCount(addressBook);i++) {
ABRecordRef myPerson =ABAddressBookGetPersonWithRecordID (addressBook,(ABRecordID)(i));
NSString *name = (NSString*)ABRecordCopyCompositeName(myPerson);
//save in database
AddressbookRecord *addObj = [[AddressbookRecord alloc] initWithPrimaryKey:0];
addObj.ClientName=name;
[addObj addNewContactEntry];
}
addressBookLoaded=2;开发者_JAVA技巧
[pool release];
}
More specifically, it points to this specific line as the problem:
NSString *name =(NSString*)ABRecordCopyCompositeName(myPerson);
My programmer can't seem to figure out what the problem is since he can't replicate it on his end. Does anyone have any ideas what would cause this problem???
Thanks!
The crash is probably cause by this line:
ABRecordRef myPerson =ABAddressBookGetPersonWithRecordID (addressBook,(ABRecordID)(i));
... returning nil owing to the specific contents of your address database on your iPhone. You would need to examine the data from the address book being returned in this chunk of code to see exactly where the problem is.
In any case, every time you fetch some data from the address book, you should be checking its integrity and watching for errors. This code just charges through always assuming every line worked perfectly.
精彩评论