在iOS中,可以为电话号码和电子邮件地址创建自定义标签.有没有办法以编程方式(使用CNContacts或ABAddressBook)删除那些创建的标签?换句话说:我不想从联系人中删除自定义标签,而是要从系统中删除自定义标签",因此当有人显示可用列表时,它根本不会显示.
附加的iOS 9源代码,可在电话簿中创建联系人,并在电子邮件字段上使用自定义标签.
func createContact(){让contactStore = CNContactStore()让newContact = CNMutableContact()newContact.givenName =克里斯"newContact.familyName =最后一个"let homeEmail = CNLabeledValue(标签:"RandomLabel",值:"IGotAnEmail@Address")newContact.emailAddresses = [homeEmail]做 {让saveRequest = CNSaveRequest()saveRequest.addContact(newContact,toContainerWithIdentifier:nil)尝试contactStore.executeSaveRequest(saveRequest)}抓住 {NSLog(保存失败")}} 解决方案Contact Framework + deleteContact
这可能会对您有所帮助.
使用此功能
我今天过得很好:
NSOperationQueue().addOperationWithBlock {[unown store] in让谓词= CNContact.predicateForContactsMatchingName("john")让toFetch = [CNContactEmailAddressesKey]做{让通讯录=尝试store.unifiedContactsMatchingPredicate(predicate,keysToFetch:toFetch)守卫contacts.count>其他0个{打印(未找到联系人")返回}//仅对符合我们条件的第一个联系人执行此操作警卫让contact = contacts.first else {返回}让req = CNSaveRequest()让mutableContact = contact.mutableCopy()为!CNMutableContactreq.deleteContact(mutableContact)做{尝试store.executeSaveRequest(req)打印(成功删除用户")}赶快让e {print(错误= \(e)")}}抓住让犯错{打印(错误)}}似乎可以,但是您需要执行以下批处理功能:
- 使用AddressBook/ABAddressBookCopyArrayOfAllPeople获取联系人
- 用于...进入联系人
- 获取ABRecordCopyValue以获取所需的ABMultiValueRef
- kABPersonEmailProperty
- kABPersonAddressProperty
- kABPersonPhoneProperty
- 为了...融入其中
- 通过ABMultiValueCopyLabelAtIndex获取当前数据
- 将其与默认标签进行比较(请注意此处获取默认值)
- 如果没有匹配项,请将其删除
希望对您有帮助
不建议使用ABAddressBook,您需要对新的联系人框架 ...玩得开心!
In iOS it is possible to create custom labels for phone numbers and email addresses. Is there a way to remove those created labels programatically (either with CNContacts or ABAddressBook)? In other words: I don't want to delete the custom label from a contact, I want to delete the "custom label" from the system so it doesn't show up at all when someone brings up the available available list.
Attached iOS 9 source code that creates a contact in the phone book with custom labels on the email field.
func createContact() { let contactStore = CNContactStore() let newContact = CNMutableContact() newContact.givenName = "Chris" newContact.familyName = "Last" let homeEmail = CNLabeledValue(label: "RandomLabel", value: "IGotAnEmail@Address") newContact.emailAddresses = [homeEmail] do { let saveRequest = CNSaveRequest() saveRequest.addContact(newContact, toContainerWithIdentifier: nil) try contactStore.executeSaveRequest(saveRequest) } catch { NSLog("Save failed") } }解决方案
Contact Framework + deleteContact
This might help you.
Using this function
EDIT: I'm in a good day:
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) } }EDIT: It seems you can but you need to do batch function like this:
- fetch contacts using AddressBook/ABAddressBookCopyArrayOfAllPeople
- For...in into contacts
- get the ABRecordCopyValue to get the ABMultiValueRef you want
- kABPersonEmailProperty
- kABPersonAddressProperty
- kABPersonPhoneProperty
- For...in into them
- Get the current with ABMultiValueCopyLabelAtIndex
- Compare it to the default labels (note here to get the default one)
- if no match, delete it
Hope it helps you
EDIT 2: Meh, ABAddressBook is deprecated, you need to do the same with New contact framework ... Have fun !!
更多推荐
以编程方式删除自定义电话标签
发布评论