zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    函数基础
    全局变量与类似配置文件的模块文件
    global语句(python学习手册422页)
    作用域实例
    变量名解析:LEGB原则
    作用域
    第三方库安装方法
    s[-1]和s[len(s)-1]
    查找特定后缀的文件
    logging日志管理-将日志写入文件
  • 原文地址:https://www.cnblogs.com/rambot/p/4868842.html
Copyright © 2011-2022 走看看