以编程方式删除自定义手机标签
问题描述:
在iOS中,可以为电话号码和电子邮件地址创建自定义标签。有没有办法以编程方式删除这些创建的标签(使用CNContacts或ABAddressBook)? 换句话说:我不想从联系人中删除自定义标签,我想从系统中删除“自定义标签”,这样当有人提供可用的可用列表时,根本不会显示它。以编程方式删除自定义手机标签
附加的iOS 9源代码,用于在电子邮件中使用自定义标签在电子邮件字段中创建联系人。
func createContact() {
let contactStore = CNContactStore()
let newContact = CNMutableContact()
newContact.givenName = "Chris"
newContact.familyName = "Last"
let homeEmail = CNLabeledValue(label: "RandomLabel", value: "[email protected]")
newContact.emailAddresses = [homeEmail]
do {
let saveRequest = CNSaveRequest()
saveRequest.addContact(newContact, toContainerWithIdentifier: nil)
try contactStore.executeSaveRequest(saveRequest)
}
catch {
NSLog("Save failed")
}
}
答
联系框架+ deleteContact
This可以帮助你。
编辑:我是一个好日子:
NSOperationQueue().addOperationWithBlock{[unowned store] in
let predicate = CNContact.predicateForContactsMatchingName("john")
let toFetch = [CNContactEmailAddressesKey]
do{
let contacts = try store.unifiedContactsMatchingPredicate(predicate,
keysToFetch: toFetch)
guard contacts.count > 0 else{
print("No contacts found")
return
}
//only do this to the first contact matching our criteria
guard let contact = contacts.first else{
return
}
let req = CNSaveRequest()
let mutableContact = contact.mutableCopy() as! CNMutableContact
req.deleteContact(mutableContact)
do{
try store.executeSaveRequest(req)
print("Successfully deleted the user")
} catch let e{
print("Error = \(e)")
}
} catch let err{
print(err)
}
}
编辑:看来你可以,但你需要做的批处理功能是这样的:
- 取联系人使用AddressBook/ABAddressBookCopyArrayOfAllPeople
- For ... in into contacts
- 得到ABRecordCopyValue得到ABMultiValueRef你想
- kABPersonEmailProperty
- kABPersonAddressProperty
- kABPersonPhoneProperty
- 对于...在放进去
- 获取当前与ABMultiValueCopyLabelAtIndex
- 比较它以默认标签(注意here to get the default one)
- 如果没有匹配,将其删除
希望它可以帮助你
编辑2:咩,ABAddressBook已被弃用,你需要做同样的New contact framework ......玩得开心!
删除电话簿中的联系人。这不是我要找的。我想从系统中删除一个自定义标签而不是联系人或更改电话簿中用户的字段。 – hashier
哦,我的错,我没有正确阅读。不知道你可以删除它:它看起来像即使你卸载一个应用程序添加自定义标签,他们留在这里 – jlngdt
是的,他们在系统中。希望有一种方法可以将它们删除。不管怎么说,还是要谢谢你。 – hashier