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


//
返回用户手机通讯录中所有联系人的手机号 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()

 

posted @ 2015-10-11 10:01  Rambot  阅读(847)  评论(0编辑  收藏  举报