我正在使用新框架 Contacts 来获取 IOS 9 中的联系人,还使用 ABAddressBook 框架来获取 IOS 8.0 中的联系人姓名,但在 IOS 8.0 应用程序中出现崩溃。我正在使用以下代码检查 IOS 版本
if #available(iOS 9.0, *)
{
/*push to Contacts framework */
}
else
{
/* push to ABAddressBook framework */
}
在 Swift 1.2 中,我正在使用此代码来获取联系人姓名,它工作正常
func processContactNames()
{
var errorRef: Unmanaged<CFError>?
var addressBook: ABAddressBookRef? = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef))
var contactList: NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue()
contactLists = []
contactEmails = []
contactEmailDetails = []
for record:ABRecordRef in contactList
{
var contactDetails: NSDictionary! = processAddressbookRecord(record)
var emailAddresses: Array<String>! = contactDetails.valueForKey("emails") as! Array
if emailAddresses.count > 0
{
for email in emailAddresses
{
var emailDetails: NSDictionary! = ["email":email, "contactIndex":contactEmailDetails.count, "contactName":contactDetails.valueForKey("contactName") as! String, "photo":contactDetails.valueForKey("photo") as! UIImage]
contactEmailDetails.append(emailDetails)
contactEmails.append(email)
}
}
}
}
在 Swift 2.0 中我正在使用这个函数。现在它在 xcode 7 中不能正常工作。
func processAddressbookRecord(addressBookRecord: ABRecordRef)->NSDictionary
{
if var contactName: String = ABRecordCopyCompositeName(addressBookRecord)?.takeRetainedValue() as? String
{
var contactName: String = ""
contactName = ABRecordCopyCompositeName(addressBookRecord).takeRetainedValue() as! String
let emailAddresses: Array<String>! = processEmail(addressBookRecord)
let photo: UIImage! = processPhoto(addressBookRecord)
let contactDetails: NSDictionary! = ["contactName":contactName,"emails":emailAddresses,"photo":photo]
print(emailAddresses.count)
return contactDetails
}
}