Swift - 使用Contacts访问通讯录2(添加、修改、删除联系人)

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
import  UIKit
import  Contacts
 
class  ViewController UIViewController  {
     
     override  func  viewDidLoad() {
         super .viewDidLoad()
         
         //申请权限
         CNContactStore ().requestAccess( for : .contacts) { (isRight, error)  in
             if  isRight {
                 //授权成功添加数据。
                 self .addContact()
             }
         }
     }
     
     //新增一个联系人
     func  addContact() {
         //创建通讯录对象
         let  store =  CNContactStore ()
         
         //创建CNMutableContact类型的实例
         let  contactToAdd =  CNMutableContact ()
         
         //设置姓名
         contactToAdd.familyName =  "张"
         contactToAdd.givenName =  "飞"
         
         //设置昵称
         contactToAdd.nickname =  "fly"
         
         //设置头像
         let  image =  UIImage (named:  "fei" )!
         contactToAdd.imageData =  UIImagePNGRepresentation (image)
         
         //设置电话
         let  mobileNumber =  CNPhoneNumber (stringValue:  "18510002000" )
         let  mobileValue =  CNLabeledValue (label:  CNLabelPhoneNumberMobile ,
                                          value: mobileNumber)
         contactToAdd.phoneNumbers = [mobileValue]
         
         //设置email
         let  email =  CNLabeledValue (label:  CNLabelHome , value:  "feifei@163.com"  as  NSString )
         contactToAdd.emailAddresses = [email]
         
         //添加联系人请求
         let  saveRequest =  CNSaveRequest ()
         saveRequest.add(contactToAdd, toContainerWithIdentifier:  nil )
         
         do {
             //写入联系人
             try store.execute(saveRequest)
             print ( "保存成功!" )
         } catch {
             print (error)
         }
     }
 
     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
import  UIKit
import  Contacts
 
class  ViewController UIViewController  {
     
     override  func  viewDidLoad() {
         super .viewDidLoad()
         
         //申请权限
         CNContactStore ().requestAccess( for : .contacts) { (isRight, error)  in
             if  isRight {
                 //授权成功修改数据。
                 self .editContact()
             }
         }
     }
     
     func  editContact() {
         //创建通讯录对象
         let  store =  CNContactStore ()
         
         //获取Fetch,并且指定要获取联系人中的什么属性
         let  keys = [ CNContactFamilyNameKey CNContactGivenNameKey CNContactNicknameKey ]
         
         //创建请求对象,需要传入一个(keysToFetch: [CNKeyDescriptor]) 包含'CNKeyDescriptor'类型的数组
         let  request =  CNContactFetchRequest (keysToFetch: keys  as  [ CNKeyDescriptor ])
         
         //遍历所有联系人
         do {
             try store.enumerateContacts(with: request, usingBlock: {
                 (contact :  CNContact , stop :  UnsafeMutablePointer < ObjCBool >) ->  Void  in
                 
                 let  mutableContact = contact.mutableCopy()  as CNMutableContact
                 
                 //获取姓名
                 let  lastName = mutableContact.familyName
                 let  firstName = mutableContact.givenName
                 //判断是否符合要求
                 if  lastName ==  "张"  && firstName ==  "飞" {
                     //设置昵称
                     mutableContact.nickname =  "小飞飞"
                     
                     //修改联系人请求
                     let  request =  CNSaveRequest ()
                     request.update(mutableContact)
                     do {
                         //修改联系人
                         try store.execute(request)
                         print ( "修改成功!" )
                     } catch {
                         print (error)
                     }
                 }
             })
         } catch {
             print (error)
         }
     }
 
     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
import  UIKit
import  Contacts
 
class  ViewController UIViewController  {
     
     override  func  viewDidLoad() {
         super .viewDidLoad()
         
         //申请权限
         CNContactStore ().requestAccess( for : .contacts) { (isRight, error)  in
             if  isRight {
                 //授权成功删除数据。
                 self .deleteContact()
             }
         }
     }
     
     func  deleteContact() {
         //创建通讯录对象
         let  store =  CNContactStore ()
         
         //获取Fetch,并且指定要获取联系人中的什么属性
         let  keys = [ CNContactFamilyNameKey CNContactGivenNameKey CNContactNicknameKey ]
         
         //创建请求对象,需要传入一个(keysToFetch: [CNKeyDescriptor]) 包含'CNKeyDescriptor'类型的数组
         let  request =  CNContactFetchRequest (keysToFetch: keys  as  [ CNKeyDescriptor ])
         
         //遍历所有联系人
         do {
             try store.enumerateContacts(with: request, usingBlock: {
                 (contact :  CNContact , stop :  UnsafeMutablePointer < ObjCBool >) ->  Void  in
                 
                 let  mutableContact = contact.mutableCopy()  as CNMutableContact
 
                 //获取姓名
                 let  lastName = mutableContact.familyName
                 let  firstName = mutableContact.givenName
                 //判断是否符合要求
                 if  lastName ==  "张"  && firstName ==  "飞" {
                     //删除联系人请求
                     let  request =  CNSaveRequest ()
                     request.delete(mutableContact)
                     
                     do {
                         //执行操作
                         try store.execute(request)
                         print ( "删除成功!" )
                     } catch {
                         print (error)
                     }
                 }
             })
         } catch {
             print (error)
         }
     }
 
     override  func  didReceiveMemoryWarning() {
         super .didReceiveMemoryWarning()
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我无法提供C语言代码。但是,我可以向您介绍如何使用Node.js模块来创建、读取、更新和删除macOS上的用户联系人数据库。 1. 创建通讯录 要创建通讯录,您需要使用Node.js模块node-mac-contacts。首先,您需要安装该模块。在终端中运行以下命令: ``` npm i node-mac-contacts ``` 安装完成后,您可以使用以下代码创建一个联系人: ```javascript const contacts = require('node-mac-contacts'); const newContact = { firstName: 'John', lastName: 'Doe', email: 'johndoe@example.com', phone: '123-456-7890' }; contacts.addContact(newContact, (err, contact) => { if (err) { console.log(err); } else { console.log('Contact created:', contact); } }); ``` 2. 显示通讯录 要显示通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); contacts.getAllContacts((err, contacts) => { if (err) { console.log(err); } else { console.log('Contacts:', contacts); } }); ``` 3. 查询通讯录 要查询通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); const query = { firstName: 'John', lastName: 'Doe' }; contacts.findContacts(query, (err, contacts) => { if (err) { console.log(err); } else { console.log('Contacts:', contacts); } }); ``` 4. 修改通讯录修改通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); const query = { firstName: 'John', lastName: 'Doe' }; const update = { email: 'newemail@example.com' }; contacts.updateContact(query, update, (err, contact) => { if (err) { console.log(err); } else { console.log('Contact updated:', contact); } }); ``` 5. 添加通讯录添加通讯录,您可以使用第1步中的代码。 6. 删除通讯录删除通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); const query = { firstName: 'John', lastName: 'Doe' }; contacts.deleteContact(query, (err) => { if (err) { console.log(err); } else { console.log('Contact deleted'); } }); ``` 7. 退出系统 要退出系统,您可以使用以下代码: ```javascript process.exit(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值