Swift - 添加、修改、删除通讯录联系人
生活随笔
收集整理的這篇文章主要介紹了
Swift - 添加、修改、删除通讯录联系人
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用AddressBook.framework框架,我們除了可以很方便的獲取通信錄里的聯系人。同時,還能對通訊錄進行新增、修改、刪除聯系人操作。
(注意:這些操作同查詢一樣,首先需要發起授權請求)1,添加新聯系人
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import UIKit import AddressBook import AddressBookUI class ViewController: UIViewController { ????? ????//address Book對象,用來獲取電話簿句柄 ????var addressBook:ABAddressBookRef? ????? ????override func viewDidLoad() { ????????super.viewDidLoad() ????????? ????????//定義一個錯誤標記對象,判斷是否成功 ????????var error:Unmanaged<CFErrorRef>? ????????addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue() ????????? ????????//發出授權信息 ????????let sysAddressBookStatus = ABAddressBookGetAuthorizationStatus() ????????if (sysAddressBookStatus == ABAuthorizationStatus.NotDetermined) { ????????????println("requesting access...") ????????????var errorRef:Unmanaged<CFError>? = nil ????????????//addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef)) ????????????ABAddressBookRequestAccessWithCompletion(addressBook, { success, error in ????????????????if success { ????????????????????//添加新聯系人 ????????????????????self.addRecord(); ????????????????} ????????????????else { ????????????????????println("error") ????????????????} ????????????}) ????????} ????????else if (sysAddressBookStatus == ABAuthorizationStatus.Denied || ????????????sysAddressBookStatus == ABAuthorizationStatus.Restricted) { ????????????????println("access denied") ????????} ????????else if (sysAddressBookStatus == ABAuthorizationStatus.Authorized) { ????????????println("access granted") ????????????//添加新聯系人 ????????????self.addRecord(); ????????} ????} ????? ????//添加新聯系人 ????func addRecord(){ ????????//定義一個錯誤標記對象,判斷是否成功 ????????var error:Unmanaged<CFErrorRef>? ????????? ????????//創建一個聯系人對象 ????????var newContact:ABRecordRef! = ABPersonCreate().takeRetainedValue() ????????var success:Bool = false ????????? ????????//設置聯系人對象昵稱 ????????success = ABRecordSetValue(newContact, kABPersonNicknameProperty, "小李", &error) ????????println("設置昵稱結果:\(success)") ????????? ????????//設置聯系人姓氏 ????????success = ABRecordSetValue(newContact, kABPersonLastNameProperty, "李", &error) ????????println("設置姓氏結果:\(success)") ????????? ????????//設置聯系人名字 ????????success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, "大木", &error) ????????println("設置名字結果:\(success)") ????????? ????????//設置聯系人電話 ????????let phones:ABMutableMultiValueRef = ABMultiValueCreateMutable( ????????????ABPropertyType(kABStringPropertyType)).takeRetainedValue() ????????success = ABMultiValueAddValueAndLabel(phones, "123456", "公司", nil) ????????println("設置電話條目:\(success)") ????????success = ABRecordSetValue(newContact, kABPersonPhoneProperty, phones, nil) ????????println("設置電話結果:\(success)") ????????? ????????//設置聯系人郵箱 ????????let addr:ABMutableMultiValueRef = ABMultiValueCreateMutable( ????????????ABPropertyType(kABStringPropertyType)).takeRetainedValue() ????????success = ABMultiValueAddValueAndLabel(addr, "ABG@hangge.com", "公司", nil) ????????println("設置郵箱條目結果:\(success)") ????????success = ABRecordSetValue(newContact, kABPersonEmailProperty, addr, nil) ????????println("設置郵箱結果:\(success)") ????????? ????????//保存聯系人 ????????success = ABAddressBookAddRecord(addressBook, newContact, &error) ????????println("保存記錄是否成功:\(success)") ????????? ????????//修改數據庫 ????????success = ABAddressBookSave(addressBook, &error) ????????println("修改數據庫是否成功:\(success)") ????} ????override func didReceiveMemoryWarning() { ????????super.didReceiveMemoryWarning() ????} } |
?
2,編輯修改聯系人先獲取所有聯系人遍歷,根據聯系人姓名或者電話來判斷是否修改
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import UIKit import AddressBook import AddressBookUI class ViewController: UIViewController { ????? ????//address Book對象,用來獲取電話簿句柄 ????var addressBook:ABAddressBookRef? ????? ????override func viewDidLoad() { ????????super.viewDidLoad() ????????? ????????//定義一個錯誤標記對象,判斷是否成功 ????????var error:Unmanaged<CFErrorRef>? ????????addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue() ????????? ????????//發出授權信息 ????????let sysAddressBookStatus = ABAddressBookGetAuthorizationStatus() ????????if (sysAddressBookStatus == ABAuthorizationStatus.NotDetermined) { ????????????println("requesting access...") ????????????var errorRef:Unmanaged<CFError>? = nil ????????????//addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef)) ????????????ABAddressBookRequestAccessWithCompletion(addressBook, { success, error in ????????????????if success { ????????????????????//編輯聯系人 ????????????????????self.editRecord(); ????????????????} ????????????????else { ????????????????????println("error") ????????????????} ????????????}) ????????} ????????else if (sysAddressBookStatus == ABAuthorizationStatus.Denied || ????????????sysAddressBookStatus == ABAuthorizationStatus.Restricted) { ????????????????println("access denied") ????????} ????????else if (sysAddressBookStatus == ABAuthorizationStatus.Authorized) { ????????????println("access granted") ????????????//編輯聯系人 ????????????self.editRecord(); ????????} ????} ????? ????//編輯聯系人 ????func editRecord(){ ????????//定義一個錯誤標記對象,判斷是否成功 ????????var error:Unmanaged<CFErrorRef>? ????????var sysContacts:NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook) ????????????.takeRetainedValue() as NSArray ????????? ????????//遍歷通訊錄 ????????for contact in sysContacts { ????????????//獲取姓名 ????????????var lastName = ABRecordCopyValue(contact, kABPersonLastNameProperty)? ????????????????.takeRetainedValue() as! String? ?? "" ????????????println("姓:\(lastName)") ????????????var firstName = ABRecordCopyValue(contact, kABPersonFirstNameProperty)? ????????????????.takeRetainedValue() as! String? ?? "" ????????????println("名:\(firstName)") ????????????? ????????????var success:Bool = false ????????????if lastName == "李" && firstName == "大木"{ ????????????????//設置聯系人新對象昵稱 ????????????????success = ABRecordSetValue(contact, kABPersonNicknameProperty, "小李子", &error) ????????????????println("設置昵稱結果:\(success)") ????????????} ????????} ????????? ????????//保存數據庫 ????????var success = ABAddressBookSave(addressBook, &error) ????????println("修改數據庫是否成功:\(success)") ????} ????? ????override func didReceiveMemoryWarning() { ????????super.didReceiveMemoryWarning() ????} } |
?
3,刪除聯系人先獲取所有聯系人遍歷,根據聯系人姓名或者電話來判斷是否刪除
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import UIKit import AddressBook import AddressBookUI class ViewController: UIViewController { ????? ????//address Book對象,用來獲取電話簿句柄 ????var addressBook:ABAddressBookRef? ????? ????override func viewDidLoad() { ????????super.viewDidLoad() ????????? ????????//定義一個錯誤標記對象,判斷是否成功 ????????var error:Unmanaged<CFErrorRef>? ????????addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue() ????????? ????????//發出授權信息 ????????let sysAddressBookStatus = ABAddressBookGetAuthorizationStatus() ????????if (sysAddressBookStatus == ABAuthorizationStatus.NotDetermined) { ????????????println("requesting access...") ????????????var errorRef:Unmanaged<CFError>? = nil ????????????//addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef)) ????????????ABAddressBookRequestAccessWithCompletion(addressBook, { success, error in ????????????????if success { ????????????????????//刪除聯系人 ????????????????????self.deleteRecord(); ????????????????} ????????????????else { ????????????????????println("error") ????????????????} ????????????}) ????????} ????????else if (sysAddressBookStatus == ABAuthorizationStatus.Denied || ????????????sysAddressBookStatus == ABAuthorizationStatus.Restricted) { ????????????????println("access denied") ????????} ????????else if (sysAddressBookStatus == ABAuthorizationStatus.Authorized) { ????????????println("access granted") ????????????//刪除聯系人 ????????????self.deleteRecord(); ????????} ????} ????? ????//刪除聯系人 ????func deleteRecord(){ ????????//定義一個錯誤標記對象,判斷是否成功 ????????var error:Unmanaged<CFErrorRef>? ????????var sysContacts:NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook) ????????????.takeRetainedValue() as NSArray ????????? ????????//遍歷通訊錄 ????????for contact in sysContacts { ????????????//獲取姓名 ????????????var lastName = ABRecordCopyValue(contact, kABPersonLastNameProperty)? ????????????????.takeRetainedValue() as! String? ?? "" ????????????var firstName = ABRecordCopyValue(contact, kABPersonFirstNameProperty)? ????????????????.takeRetainedValue() as! String? ?? "" ????????????? ????????????var success:Bool = false ????????????if lastName == "李" && firstName == "大木"{ ????????????????success = ABAddressBookRemoveRecord(addressBook, contact, nil) ????????????????println("刪除結果:\(success)") ????????????} ????????} ????????? ????????//保存數據庫 ????????var success = ABAddressBookSave(addressBook, &error) ????????println("修改數據庫是否成功:\(success)") ????} ????? ????override func didReceiveMemoryWarning() { ????????super.didReceiveMemoryWarning() ????} } |
轉載于:https://www.cnblogs.com/Free-Thinker/p/4843599.html
總結
以上是生活随笔為你收集整理的Swift - 添加、修改、删除通讯录联系人的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Swift - 选择框(UIPicker
- 下一篇: hihocoder#1054 : 滑动解