首页 > 移动平台 > 详细

ios访问手机通讯录获取联系人手机号

时间:2015-10-11 10:14:04      阅读:2244      评论:0      收藏:0      [点我收藏+]

//
返回用户手机通讯录中所有联系人的手机号 func phoneNumbersFromContacts() throws -> [String]{ var phoneNumbers: [String] = [String]() //获取授权状态, 如果没有授权,那么请求授权 let status: CNAuthorizationStatus = CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts) if status == CNAuthorizationStatus.NotDetermined{ // 创建CNContactStore对象, 并请求授权 let store = CNContactStore() store.requestAccessForEntityType(.Contacts, completionHandler: { (granted: Bool, error: NSError?) -> Void in if error != nil { return } if granted{ print("访问通讯录授权成功") }else{ print("访问通讯录授权失败") } }) } //姓,名,号码 let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey] let request = CNContactFetchRequest(keysToFetch: keys) //请求所有联系人 let store = CNContactStore() try store.enumerateContactsWithFetchRequest(request) { (contact:CNContact, stop:UnsafeMutablePointer<ObjCBool>) -> Void in for value in contact.phoneNumbers{ if value.label == "_$!<Mobile>!$_"{ let phoneNum = value.value as! CNPhoneNumber phoneNumbers.append(phoneNum.stringValue) } } } return phoneNumbers
}

 

调用:

 

 let a: [String] =  try! phoneNumbersFromContacts()

 

ios访问手机通讯录获取联系人手机号

原文:http://www.cnblogs.com/rambot/p/4868842.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!