Swift iOS 9通讯录访问
原创Blog,转载请注明出处
文档
Demo效果下载链接 请求访问权限相关类
CNContact(线程安全)
示例代码 class ContactsStore{
static let sharedStore = CNContactStore()
}
请求权限 let authStatus = CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts)
let authStatus = ContactsStore.sharedStore.authorizationStatusForEntityType(CNEntityType.Contacts)
if authStatus == CNAuthorizationStatus.Denied || authStatus == CNAuthorizationStatus.NotDetermined{
self.contactsStore.requestAccessForEntityType(CNEntityType.Contacts,completionHandler: { (result,error) -> Void in
if result == false{
let alert = UIAlertController(title: "警告",message: "请在设置中允许通讯录访问,否则App无法正常使用",preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "确定",style: UIAlertActionStyle.Cancel,handler: nil))
self.presentViewController(alert,animated: true,completion: nil)
}
})
}
调用系统的ContactsPickerViewController
func contactPicker(picker: CNContactPickerViewController,didSelectContact contact: CNContact) {
if let phoneNumber = contact.phoneNumbers.first?.value as? CNPhoneNumber{
self.textfield.text = phoneNumber.stringValue
}
}
然后,模态展示 let contactsVC = CNContactPickerViewController()
contactsVC.delegate = self;
presentViewController(contactsVC,animated:true,completion: nil)
查询全部通讯录注意,要先指明需要fetch的属性, 因为 let keys = [CNContactGivenNameKey,CNContactPhoneNumbersKey,CNContactThumbnailImageDataKey]
let fetchAllRequest = CNContactFetchRequest(keysToFetch:keys)
do{
try ContactsStore.sharedStore.enumerateContactsWithFetchRequest(fetchAllRequest) { (contact,pointer) -> Void in
self.contacts.append(contact)
}
}catch{
}
条件查询条件查询在示例工程中未列出,按照如下步骤查询 + predicateForContactsMatchingName:
+ predicateForContactsWithIdentifiers:
...
使用CNContactStore的 添加摘自Swift2.1文档 import Contacts
// Creating a mutable object to add to the contact
let contact = CNMutableContact()
contact.imageData = NSData() // The profile picture as a NSData object
contact.givenName = "John"
contact.familyName = "Appleseed"
let homeEmail = CNLabeledValue(label:CNLabelHome,value:"john@example.com")
let workEmail = CNLabeledValue(label:CNLabelWork,value:"j.appleseed@icloud.com")
contact.emailAddresses = [homeEmail,workEmail]
contact.phoneNumbers = [CNLabeledValue(
label:CNLabelPhoneNumberiPhone,value:CNPhoneNumber(stringValue:"(408) 555-0126"))]
let homeAddress = CNMutablePostalAddress()
homeAddress.street = "1 Infinite Loop"
homeAddress.city = "Cupertino"
homeAddress.state = "CA"
homeAddress.postalCode = "95014"
contact.postalAddresses = [CNLabeledValue(label:CNLabelHome,value:homeAddress)]
let birthday = NSDateComponents()
birthday.day = 1
birthday.month = 4
birthday.year = 1988 // You can omit the year value for a yearless birthday
contact.birthday = birthday
// Saving the newly created contact
let store = CNContactStore()
let saveRequest = CNSaveRequest()
saveRequest.addContact(contact,toContainerWithIdentifier:nil)
try store.executeSaveRequest(saveRequest)
本地化/格式化几个常用的类
举例 从Contact中拼接出全名 let fullName = CNContactFormatter.stringFromContact(contact,style: .FullName)
print(fullName)
// John Appleseed
邮政地址 let postalString = CNPostalAddressFormatter.stringFromPostalAddress(homeAddress)
print(postalString)
// 1 Infinite Loop
// Cupertino
// CA
// 95014
对通讯录的内置Key获取 let displayName = CNContact.localizedStringForKey(CNContactNicknameKey)
print(displayName)
// 昵称,在中文条件下
最后欢迎关注我的CSDN博客,在我每个月都会更新10篇左右的iOS 文章,源码都是Swift的。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |