When I attempt to create an ABPerson
with ABPersonCreatePeopleInSourceWithVCardRepresentation()
using corrupt vCard data my app crashes. This is the console output:
2012-07-14 20:20:37.530 vCardTest[4418:707] vCard Syntax Error, character: 7 : /
2012-07-14 20:20:37.531 vCardTest[4418:707] Malformed BEGIN
2012-07-14 20:20:37.534 vCardTest[4418:707] vCard Syntax Error, character: 14 : n
2012-07-14 20:20:37.536 vCardTest[4418:707] Malformed BEGIN
2012-07-14 20:20:37.539 vCardTest[4418:707] vCard Syntax Error, character: 21 : i
2012-07-14 20:20:37.543 vCardTest[4418:707] Malformed BEGIN
2012-07-14 20:20:37.546 vCardTest[4418:707] vCard Syntax Error, character: 28 : o
2012-07-14 20:20:37.548 vCardTest[4418:707] Malformed BEGIN
2012-07-14 20:20:37.550 vCardTest[4418:707] vCard Syntax Error, character: 35 : s
2012-07-14 20:20:37.553 vCardTest[4418:707] Malformed BEGIN
2012-07-14 20:20:37.555 vCardTest[4418:707] vCard Syntax Error, character: 42 : g
2012-07-14 20:20:37.556 vCardTest[4418:707] Malformed BEGIN
2012-07-14 20:20:37.559 vCardTest[4418:707] Malformed BEGIN
@try
and @catch
but no exception is really thrown.The input string that generates the vCard comes from different sources which I do not control, so a lot of the times the input data won't even be a vCard at all. I'm only concerned about parsing valid vCards that's why it is important to be able to detect non-vCard data and discard it.
I faced exactly the same problem, did a lot of research, but couldn't find any way to catch the exception either.
Since the errors don't seem to influence my app, I simply ignore them and check how many valid vCards were found in the scanned data - in my case the vCard comes from a QR code and I expect it to contain exactly one vCard.
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, scannedVCard);
if(vCardPeople != NULL && CFArrayGetCount(vCardPeople) == 1) {
// Successfully read one vCard, save it to contacts
ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, 0);
// ...
}
else {
// Display an alert view to inform the user about malformed input data
}
You said your app crashes - I think this is only indirectly related to the malformed vCard. Maybe it doesn't crash until a few lines later when your code is not prepared to handle the empty array.