我正在尝试使用Swift获取我的应用中所有姓名和电话号码的列表。它运行完美,我发现标准调试版本没有错误,但是当我推送到应用程序商店并使用发布版本时,从通讯录中获取联系人时,应用程序总是崩溃。我检查了调试版本在尝试实际访问它们之前请求联系人,所以我没有看到任何明显的错误。
我的加载联系人的代码如下:
func getAllContacts() {
var errorRef: Unmanaged<CFError>? = nil
var addressBookRef = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef))
if (ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.NotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, { success, error in
if (success) {
self.loadContacts()
}
else {
var alertView = UIAlertView(title: "Error", message:"Please allow Favour to access to your contacts", delegate:nil, cancelButtonTitle: "OK")
alertView.show()
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.Authorized) {
self.loadContacts()
}
else {
var alertView = UIAlertView(title: "Error", message:"Please allow Favour to access to your contacts", delegate:nil, cancelButtonTitle: "OK")
alertView.show()
}
}
func loadContacts() {
var errorRef: Unmanaged<CFError>?
var addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef))
var contactList: NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue()
for record:ABRecordRef in contactList {
var contactPerson: ABRecordRef = record
if(ABRecordCopyCompositeName(contactPerson)? == nil) {
continue
}
var contactName: String = ABRecordCopyCompositeName(contactPerson).takeRetainedValue() as NSString
let phoneProperty: ABMultiValueRef = ABRecordCopyValue(record, kABPersonPhoneProperty).takeRetainedValue() as ABMultiValueRef
if (ABMultiValueGetCount(phoneProperty) > 0) {
let allPhones : NSArray = ABMultiValueCopyArrayOfAllValues(phoneProperty).takeUnretainedValue() as NSArray
for phone in allPhones {
let phoneString = phone as String
var newDictionary = NSMutableDictionary()
newDictionary["name"] = contactName.lowercaseString
newDictionary["phone"] = phoneString
contacts.addObject(newDictionary)
}
}
}
contacts = filterInvites(contacts)
contacts = NSMutableArray(array: (contacts as Array).sorted {($0["name"] as String) < ($1["name"] as String)})
tableView.reloadData()
}
这是来自App Store的发布版本的符号化堆栈跟踪
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 Favour
0x000e2f48 reabstraction thunk helper from
@callee_owned (@owned Swift.AnyObject, @owned Swift.AnyObject) -> (@unowned Swift.Bool) to @callee_owned (@in Swift.AnyObject, @in Swift.AnyObject) -> (@unowned Swift.Bool) with unmangled suffix "_constprop0" (DetailViewController.swift:285)
1 Favour 0x000e5b1c specialization <Swift.UnsafeMutableBufferPointer<Swift.AnyObject> with Swift.UnsafeMutableBufferPointer<Swift.AnyObject> : Swift.MutableCollectionType, Swift.UnsafeBufferPointerGenerator<Swift.AnyObject> with Swift.UnsafeBufferPointerGenerator<Swift.AnyObject> : Swift.GeneratorType, Swift.AnyObject, Swift.Int with Swift.Int : Swift.RandomAccessIndexType, Swift.Int with Swift.Int : Swift._SignedIntegerType, Swift.Int with Swift.Int : Swift._BuiltinIntegerLiteralConvertible, Swift.Int with Swift.Int : Swift.SignedNumberType, Swift.Int with Swift.Int : Swift._BuiltinIntegerLiteralConvertible, Swift.Int, Swift.AnyObject> of Swift._introSortImpl <A : Swift.MutableCollectionType>(inout A, Swift.Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Swift.Bool, Swift.Int) -> () (DetailViewController.swift:0)
2 Favour 0x000e4e44 specialization <Swift.AnyObject> of Swift.Array.sort <A>(inout [A])((A, A) -> Swift.Bool) -> () (DetailViewController.swift:0)
3 Favour 0x000e8a34 Favour.InviteViewController.loadContacts (Favour.InviteViewController)() -> () (InviteViewController.swift:0)
4 Favour 0x000e7960 @objc Favour.InviteViewController.viewDidLoad (Favour.InviteViewController)() -> () (InviteViewController.swift:74)
...
有没有人知道为什么每次尝试加载联系人时都会崩溃?我无法在调试中重现它,所以我对这一点感到难过。