mercredi 4 mars 2015

App occasionally crashes reading contacts

My app is trying to access the contacts list (names and phone numbers) and it works correctly every time on iOS simulator, but occasionally crashes on iPhones.



for i in 0...4 {
//First two or three requests return inconsistent data. This drops 4 sets. I have no idea why this is necessary

let status = ABAddressBookGetAuthorizationStatus()

if status == .Denied || status == .Restricted {
return
}

var error: Unmanaged<CFError>?
var addressBook: ABAddressBook? = ABAddressBookCreateWithOptions(nil, &error)?.takeRetainedValue()
if addressBook == nil {
println(error?.takeRetainedValue())
return
}

ABAddressBookRequestAccessWithCompletion(addressBook) {
granted, error in

if granted {
let people:[ABRecordRef] = ABAddressBookCopyArrayOfAllPeople(addressBook)?.takeRetainedValue() as [ABRecordRef]

for person in people {
if ABRecordCopyCompositeName(person)? == nil {
Globals.people = []
Globals.phones = [String: String]()
break
}
var phones: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
if ABMultiValueGetCount(phones) > 0 {
var phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, 0)
Globals.people.append(ABRecordCopyCompositeName(person).takeRetainedValue() as String)
Globals.phones.updateValue(phoneNumberRef.takeRetainedValue() as String, forKey: ABRecordCopyCompositeName(person).takeRetainedValue() as String)
}
}
}
}
}


I have no USB cable (original USB cable broke, now using one part charger), so I don't know what error is returned on iPhones, but the code works even without the uppermost loop on the iOS simulator.


What could be causing the error? Follow up, why is the loop necessary, i.e. why is the code returning inconsistent data first few runs?




Aucun commentaire:

Enregistrer un commentaire