2015-10-05 84 views
1

在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") 
    } 
} 

回答

0

聯繫框架+ deleteContact

This可以幫助你。

使用this function

編輯:我是一個好日子:

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 ......玩得開心!

+0

刪除電話簿中的聯繫人。這不是我要找的。我想從系統中刪除一個自定義標籤而不是聯繫人或更改電話簿中用戶的字段。 – hashier

+0

哦,我的錯,我沒有正確閱讀。不知道你可以刪除它:它看起來像即使你卸載一個應用程序添加自定義標籤,他們留在這裏 – jlngdt

+0

是的,他們在系統中。希望有一種方法可以將它們刪除。不管怎麼說,還是要謝謝你。 – hashier