swift获取通讯录
生活随笔
收集整理的這篇文章主要介紹了
swift获取通讯录
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、在工程中添加AddressBook.framework和AddressBookUI.framework
二、首先在代碼中添加如下
import AddressBook import AddressBookUI?
三、在添加以下封裝好的方法
func getSysContacts() -> [[String:AnyObject]] {var error:Unmanaged<CFError>?var addressBook: ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()let sysAddressBookStatus = ABAddressBookGetAuthorizationStatus()if sysAddressBookStatus == .Denied || sysAddressBookStatus == .NotDetermined {// Need to ask for authorizationvar authorizedSingal:dispatch_semaphore_t = dispatch_semaphore_create(0)var askAuthorization:ABAddressBookRequestAccessCompletionHandler = { success, error inif success {ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArraydispatch_semaphore_signal(authorizedSingal)}}ABAddressBookRequestAccessWithCompletion(addressBook, askAuthorization)dispatch_semaphore_wait(authorizedSingal, DISPATCH_TIME_FOREVER)}func analyzeSysContacts(sysContacts:NSArray) -> [[String:AnyObject]] {var allContacts:Array = [[String:AnyObject]]()func analyzeContactProperty(contact:ABRecordRef, property:ABPropertyID) -> [AnyObject]? {var propertyValues:ABMultiValueRef? = ABRecordCopyValue(contact, property)?.takeRetainedValue()if propertyValues != nil {var values:Array<AnyObject> = Array()for i in 0 ..< ABMultiValueGetCount(propertyValues) {var value = ABMultiValueCopyValueAtIndex(propertyValues, i)switch property {// 地址case kABPersonAddressProperty :var valueDictionary:Dictionary = [String:String]()var addrNSDict:NSMutableDictionary = value.takeRetainedValue() as NSMutableDictionaryvalueDictionary["_Country"] = addrNSDict.valueForKey(kABPersonAddressCountryKey) as? String ?? ""valueDictionary["_State"] = addrNSDict.valueForKey(kABPersonAddressStateKey) as? String ?? ""valueDictionary["_City"] = addrNSDict.valueForKey(kABPersonAddressCityKey) as? String ?? ""valueDictionary["_Street"] = addrNSDict.valueForKey(kABPersonAddressStreetKey) as? String ?? ""valueDictionary["_Contrycode"] = addrNSDict.valueForKey(kABPersonAddressCountryCodeKey) as? String ?? ""// 地址整理var fullAddress:String = (valueDictionary["_Country"]! == "" ? valueDictionary["_Contrycode"]! : valueDictionary["_Country"]!) + ", " + valueDictionary["_State"]! + ", " + valueDictionary["_City"]! + ", " + valueDictionary["_Street"]!values.append(fullAddress)// SNScase kABPersonSocialProfileProperty :var valueDictionary:Dictionary = [String:String]()var snsNSDict:NSMutableDictionary = value.takeRetainedValue() as NSMutableDictionaryvalueDictionary["_Username"] = snsNSDict.valueForKey(kABPersonSocialProfileUsernameKey) as? String ?? ""valueDictionary["_URL"] = snsNSDict.valueForKey(kABPersonSocialProfileURLKey) as? String ?? ""valueDictionary["_Serves"] = snsNSDict.valueForKey(kABPersonSocialProfileServiceKey) as? String ?? ""values.append(valueDictionary)// IMcase kABPersonInstantMessageProperty :var valueDictionary:Dictionary = [String:String]()var imNSDict:NSMutableDictionary = value.takeRetainedValue() as NSMutableDictionaryvalueDictionary["_Serves"] = imNSDict.valueForKey(kABPersonInstantMessageServiceKey) as? String ?? ""valueDictionary["_Username"] = imNSDict.valueForKey(kABPersonInstantMessageUsernameKey) as? String ?? ""values.append(valueDictionary)// Datecase kABPersonDateProperty :var date:String? = (value.takeRetainedValue() as? NSDate)?.descriptionif date != nil {values.append(date!)}default :var val:String = value.takeRetainedValue() as? String ?? ""values.append(val)}}return values}else{return nil}}for contact in sysContacts {var currentContact:Dictionary = [String:AnyObject]()/*部分單值屬性*/// 姓、姓氏拼音var FirstName:String = ABRecordCopyValue(contact, kABPersonFirstNameProperty)?.takeRetainedValue() as String? ?? ""currentContact["FirstName"] = FirstNamecurrentContact["FirstNamePhonetic"] = ABRecordCopyValue(contact, kABPersonFirstNamePhoneticProperty)?.takeRetainedValue() as String? ?? ""// 名、名字拼音var LastName:String = ABRecordCopyValue(contact, kABPersonLastNameProperty)?.takeRetainedValue() as String? ?? ""currentContact["LastName"] = LastNamecurrentContact["LirstNamePhonetic"] = ABRecordCopyValue(contact, kABPersonLastNamePhoneticProperty)?.takeRetainedValue() as String? ?? ""// 昵稱currentContact["Nikename"] = ABRecordCopyValue(contact, kABPersonNicknameProperty)?.takeRetainedValue() as String? ?? ""// 姓名整理currentContact["fullName"] = LastName + FirstName// 公司(組織)currentContact["Organization"] = ABRecordCopyValue(contact, kABPersonOrganizationProperty)?.takeRetainedValue() as String? ?? ""// 職位currentContact["JobTitle"] = ABRecordCopyValue(contact, kABPersonJobTitleProperty)?.takeRetainedValue() as String? ?? ""// 部門currentContact["Department"] = ABRecordCopyValue(contact, kABPersonDepartmentProperty)?.takeRetainedValue() as String? ?? ""// 備注currentContact["Note"] = ABRecordCopyValue(contact, kABPersonNoteProperty)?.takeRetainedValue() as String? ?? ""// 生日(類型轉換有問題,不可用)//currentContact["Brithday"] = ((ABRecordCopyValue(contact, kABPersonBirthdayProperty)?.takeRetainedValue()) as NSDate).description/*部分多值屬性*/// 電話var Phone:Array<AnyObject>? = analyzeContactProperty(contact, kABPersonPhoneProperty)if Phone != nil {currentContact["Phone"] = Phone}// 地址var Address:Array<AnyObject>? = analyzeContactProperty(contact, kABPersonAddressProperty)if Address != nil {currentContact["Address"] = Address}// E-mailvar Email:Array<AnyObject>? = analyzeContactProperty(contact, kABPersonEmailProperty)if Email != nil {currentContact["Email"] = Email}// 紀念日var Date:Array<AnyObject>? = analyzeContactProperty(contact, kABPersonDateProperty)if Date != nil {currentContact["Date"] = Date}// URLvar URL:Array<AnyObject>? = analyzeContactProperty(contact, kABPersonURLProperty)if URL != nil{currentContact["URL"] = URL}// SNSvar SNS:Array<AnyObject>? = analyzeContactProperty(contact, kABPersonSocialProfileProperty)if SNS != nil {currentContact["SNS"] = SNS}allContacts.append(currentContact)}return allContacts}return analyzeSysContacts( ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray )}?四、在?viewDidLoad 方法中調用?
var array:Array<Dictionary<String,AnyObject>> = getSysContacts() for contact in array {println(contact) }?
轉載于:https://www.cnblogs.com/maple023/p/4272676.html
總結
以上是生活随笔為你收集整理的swift获取通讯录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WP8.1学习系列(第八章)——透视Pi
- 下一篇: Foundation框架: 9.OC中的