iOS ABAddressBook 如何更改现有phone 号码?
iOS ABAddressBook How to change existing phone number?
我想检查联系人是否存在,如果存在则更改其 phone 号码。
我尝试这样做并在最后一行代码中得到exc_bad_access
:
if doesPersonExistWithFirstName(firstName: "Call Recorder", inAddressBook: adbk)
{
let existingContact: ABRecord! = ABAddressBookGetPersonWithRecordID(adbk, recordID).takeRetainedValue()
var success: Bool = false
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABRecordSetValue(existingContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, existingContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
else
{
let newContact: ABRecord! = ABPersonCreate().takeRetainedValue()
var success: Bool = false
let newFirstName: NSString = "Call Recorder"
let image: UIImage = UIImage(named: "record")!
let imageData = UIImagePNGRepresentation(image)
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, newFirstName as CFTypeRef, &error)
print("setting first name was successful? \(success)")
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABPersonSetImageData(newContact, imageData as CFData!, &error)
print("setting image successful? \(success)")
success = ABRecordSetValue(newContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, newContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
} **// I get exception here**
代码有效,phone 数字正确更改,但每次 运行 时应用程序崩溃。
更新: 我有 运行 僵尸工具,它给了我这个:
An Objective-C message was sent to a deallocated 'CPRecord' object (zombie) at address: 0x7c6a3200.
UPD2: 我发现这一行使应用程序崩溃:
let existingContact: ABRecord! = ABAddressBookGetPersonWithRecordID(adbk, recordID).takeRetainedValue()
可能是什么问题?
我认为问题出在这一行:
let existingContact: ABRecord! = ABAddressBookGetPersonWithRecordID(adbk, recordID).takeRetainedValue()
我改变了我的功能,所以现在它使用 ABRecord
而不是 ABRecordID
来访问所需的记录。
固定码:
if doesPersonExistWithFirstName(firstName: "Call Recorder", inAddressBook: adbk)
{
var success: Bool = false
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABRecordSetValue(existingContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, existingContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
else
{
let newContact: ABRecord! = ABPersonCreate().takeRetainedValue()
var success: Bool = false
let newFirstName: NSString = "Call Recorder"
let image: UIImage = UIImage(named: "record")!
let imageData = UIImagePNGRepresentation(image)
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, newFirstName as CFTypeRef, &error)
print("setting first name was successful? \(success)")
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABPersonSetImageData(newContact, imageData as CFData!, &error)
print("setting image successful? \(success)")
success = ABRecordSetValue(newContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, newContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
existingContacts
实施:
var existingContact: ABRecord! = ABPersonCreate().takeRetainedValue()
func doesPersonExistWithFirstName(firstName paramFirstName: String,
inAddressBook addressBook: ABAddressBook) -> Bool
{
let people = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray as [ABRecord]
for person: ABRecord in people
{
let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue() as! String
if firstName == paramFirstName
{
existingContact = person
return true
}
}
return false
}
我想检查联系人是否存在,如果存在则更改其 phone 号码。
我尝试这样做并在最后一行代码中得到exc_bad_access
:
if doesPersonExistWithFirstName(firstName: "Call Recorder", inAddressBook: adbk)
{
let existingContact: ABRecord! = ABAddressBookGetPersonWithRecordID(adbk, recordID).takeRetainedValue()
var success: Bool = false
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABRecordSetValue(existingContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, existingContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
else
{
let newContact: ABRecord! = ABPersonCreate().takeRetainedValue()
var success: Bool = false
let newFirstName: NSString = "Call Recorder"
let image: UIImage = UIImage(named: "record")!
let imageData = UIImagePNGRepresentation(image)
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, newFirstName as CFTypeRef, &error)
print("setting first name was successful? \(success)")
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABPersonSetImageData(newContact, imageData as CFData!, &error)
print("setting image successful? \(success)")
success = ABRecordSetValue(newContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, newContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
} **// I get exception here**
代码有效,phone 数字正确更改,但每次 运行 时应用程序崩溃。
更新: 我有 运行 僵尸工具,它给了我这个:
An Objective-C message was sent to a deallocated 'CPRecord' object (zombie) at address: 0x7c6a3200.
UPD2: 我发现这一行使应用程序崩溃:
let existingContact: ABRecord! = ABAddressBookGetPersonWithRecordID(adbk, recordID).takeRetainedValue()
可能是什么问题?
我认为问题出在这一行:
let existingContact: ABRecord! = ABAddressBookGetPersonWithRecordID(adbk, recordID).takeRetainedValue()
我改变了我的功能,所以现在它使用 ABRecord
而不是 ABRecordID
来访问所需的记录。
固定码:
if doesPersonExistWithFirstName(firstName: "Call Recorder", inAddressBook: adbk)
{
var success: Bool = false
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABRecordSetValue(existingContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, existingContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
else
{
let newContact: ABRecord! = ABPersonCreate().takeRetainedValue()
var success: Bool = false
let newFirstName: NSString = "Call Recorder"
let image: UIImage = UIImage(named: "record")!
let imageData = UIImagePNGRepresentation(image)
let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
var error: Unmanaged<CFError>? = nil
success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, newFirstName as CFTypeRef, &error)
print("setting first name was successful? \(success)")
success = ABMultiValueAddValueAndLabel(phoneNumbers, recordServicePhoneNumber!, kABPersonPhoneMainLabel, nil)
print("setting phone number successful? \(success)")
success = ABPersonSetImageData(newContact, imageData as CFData!, &error)
print("setting image successful? \(success)")
success = ABRecordSetValue(newContact, kABPersonPhoneProperty, phoneNumbers, &error)
print("adding phone numbers successful? \(success)")
success = ABAddressBookAddRecord(adbk, newContact, &error)
print("Adbk addRecord successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Adbk Save successful? \(success)")
}
existingContacts
实施:
var existingContact: ABRecord! = ABPersonCreate().takeRetainedValue()
func doesPersonExistWithFirstName(firstName paramFirstName: String,
inAddressBook addressBook: ABAddressBook) -> Bool
{
let people = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray as [ABRecord]
for person: ABRecord in people
{
let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue() as! String
if firstName == paramFirstName
{
existingContact = person
return true
}
}
return false
}