当我尝试保存联系时,应用程序崩溃。
private let SharedInsance = AddressBook()
class AddressBook: NSObject {
private let authorizationStatus = ABAddressBookGetAuthorizationStatus()
private let addressBookRef: ABAddressBook?
class var sharedInsance : AddressBook{
return SharedInsance
}
override init() {
addressBookRef = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()
super.init()
}
private func makeAndAddMemberRecord(member: Member) -> String {
let memberRecord: ABRecordRef = ABPersonCreate().takeRetainedValue()
if let firstName = member.firstName{
ABRecordSetValue(memberRecord, kABPersonFirstNameProperty,firstName, nil)
}
if let lastName = member.lastName{
ABRecordSetValue(memberRecord, kABPersonLastNameProperty,lastName, nil)
}
if let professionalName = member.professionalName{
ABRecordSetValue(memberRecord, kABPersonJobTitleProperty,professionalName, nil)
ABRecordSetValue(memberRecord, kABPersonOrganizationProperty, professionalName, nil)
}
if let emailId = member.emailId{
let emails: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
ABMultiValueAddValueAndLabel(emails, emailId, kABPersonAnniversaryLabel, nil)
ABRecordSetValue(memberRecord, kABPersonEmailProperty,emails, nil)
}
if let dob = member.dob {
if dob.doubleValue > 0 {
let date = Utility.UnixToDate(dob.doubleValue)
ABRecordSetValue(memberRecord, kABPersonBirthdayProperty, date as CFDateRef, nil)
}
}
if let aniversaryDate = member.aniversaryDate {
if aniversaryDate.doubleValue > 0 {
let date = Utility.UnixToDate(aniversaryDate.doubleValue)
let dates: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiDateTimePropertyType)).takeRetainedValue()
ABMultiValueAddValueAndLabel(dates, date, kABPersonAnniversaryLabel, nil)
ABRecordSetValue(memberRecord, kABPersonDateProperty, dates, nil)
}
}
//Phone Number
if let mobileNo = member.mobileNo{
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
ABMultiValueAddValueAndLabel(phoneNumbers, mobileNo, kABPersonPhoneMobileLabel, nil)
ABRecordSetValue(memberRecord, kABPersonPhoneProperty, phoneNumbers, nil)
}
//end Phone Number
//Address
let address: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiDictionaryPropertyType)).takeRetainedValue()
var addressString = ""
if let address1 = member.address1{
addressString += " " + address1
}
if let address2 = member.address2{
addressString += " " + address2
}
if let address3 = member.address3{
addressString += " " + address3
}
if !addressString.isEmpty{
ABMultiValueAddValueAndLabel(address, addressString, kABPersonAddressStreetKey, nil)
}
if let cityName = member.cityName{
ABMultiValueAddValueAndLabel(address, cityName, kABPersonAddressCityKey, nil)
}
if let stateName = member.stateName{
ABMultiValueAddValueAndLabel(address, stateName, kABPersonAddressStateKey, nil)
}
if let countryName = member.countryName{
ABMultiValueAddValueAndLabel(address, countryName, kABPersonAddressCountryKey, nil)
}
if let zipCode = member.zipCode{
ABMultiValueAddValueAndLabel(address, zipCode, kABPersonAddressZIPKey, nil)
}
ABRecordSetValue(memberRecord, kABPersonAddressProperty, address, nil)
//end Address
ABAddressBookAddRecord(addressBookRef, memberRecord, nil)
return saveAddressBookChanges()//memberRecord
}
private func saveAddressBookChanges() -> String {
var status = ""
if ABAddressBookHasUnsavedChanges(addressBookRef){
var err: Unmanaged<CFErrorRef>? = nil
let savedToAddressBook = ABAddressBookSave(addressBookRef, &err)
if savedToAddressBook {
status = "Successully saved changes."
print("Successully saved changes.")
} else {
status = "Couldn't save changes."
print("Couldn't save changes.")
}
} else {
status = "No changes occurred."
print("No changes occurred.")
}
return status
}
}
2015-12-31 12:24:44.500 KGA[2939:75016] -[Swift._NSContiguousString count]: unrecognized selector sent to instance 0x7fb1b2f1b740
2015-12-31 12:25:01.316 KGA[2939:75016] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._NSContiguousString count]: unrecognized selector sent to instance 0x7fb1b2f1b740'
*** First throw call stack:
(
0 CoreFoundation 0x0000000107066f45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000106454deb objc_exception_throw + 48
2 CoreFoundation 0x000000010706f56d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000106fbceea ___forwarding___ + 970
4 CoreFoundation 0x0000000106fbca98 _CF_forwarding_prep_0 + 120
5 AddressBook 0x00000001053f3622 ABCDBContextSaveMultiValue + 1446
6 AppSupport 0x000000010d12beb3 CPRecordStoreSaveRecord + 1399
7 CoreFoundation 0x0000000106f5e446 __CFDictionaryApplyFunction_block_invoke + 22
8 CoreFoundation 0x0000000106f5e400 CFBasicHashApply + 128
9 CoreFoundation 0x0000000106f5e33a CFDictionaryApplyFunction + 186
10 AppSupport 0x000000010d12ceae _CPRecordStoreSaveAddedRecords + 76
11 AppSupport 0x000000010d12cc85 CPRecordStoreSaveWithPreAndPostCallbacksAndTransactionType + 313
12 AddressBook 0x00000001053e438d ABCAddressBookSaveWithConflictPolicy + 1751
13 AddressBook 0x00000001053fce3e ABAddressBookSave + 84
14 KGA 0x00000001049f10f2 _TFC3KGA11AddressBookP33_19518E706057D2CD55E2EE9EBB4CCBB622saveAddressBookChangesfS0_FT_SS + 178
15 KGA 0x00000001049f0fd1 _TFC3KGA11AddressBookP33_19518E706057D2CD55E2EE9EBB4CCBB622makeAndAddMemberRecordfS0_FCS_6MemberSS + 6113
16 KGA 0x00000001049ee751 _TFC3KGA11AddressBookP33_19518E706057D2CD55E2EE9EBB4CCBB619addMemberToContactsfS0_FCS_6MemberT_ + 225
17 KGA 0x00000001049f3b9b _TFFFC3KGA11AddressBook33promptForAddressBookRequestAccessFS0_FGSQCS_6Member_T_U_FTSbGSQCSo7CFError__T_U_FT_T_ + 603
18 KGA 0x00000001048e0af7 _TTRXFo__dT__XFdCb__dT__ + 39
19 libdispatch.dylib 0x0000000109982e5d _dispatch_call_block_and_release + 12
20 libdispatch.dylib 0x00000001099a349b _dispatch_client_callout + 8
21 libdispatch.dylib 0x000000010998b2af _dispatch_main_queue_callback_4CF + 1738
22 CoreFoundation 0x0000000106fc72e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
23 CoreFoundation 0x0000000106f888a9 __CFRunLoopRun + 2073
24 CoreFoundation 0x0000000106f87e08 CFRunLoopRunSpecific + 488
25 GraphicsServices 0x000000010b209ad2 GSEventRunModal + 161
26 UIKit 0x00000001078b230d UIApplicationMain + 171
27 KGA
0x000000010493004d main + 109
28 libdyld.dylib 0x00000001099d892d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException