import UIKit
//導(dǎo)入框架
import AddressBook
import AddressBookUI
class ViewController: UIViewController {
//address Book對象坊谁,用來獲取電話簿句柄
var addressBook:ABAddressBookRef?
override func viewDidLoad() {
super.viewDidLoad()
//定義一個錯誤標(biāo)記對象嗓蘑,判斷是否成功
var error:Unmanaged<CFErrorRef>?
addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()
//發(fā)出授權(quán)信息
let sysAddressBookStatus = ABAddressBookGetAuthorizationStatus()
if (sysAddressBookStatus == ABAuthorizationStatus.NotDetermined) {
println("requesting access...")
var errorRef:Unmanaged<CFError>? = nil
//addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef))
ABAddressBookRequestAccessWithCompletion(addressBook, { success, error in
if success {
//獲取并遍歷所有聯(lián)系人記錄
self.readRecords();
}
else {
println("error")
}
})
}
else if (sysAddressBookStatus == ABAuthorizationStatus.Denied ||
sysAddressBookStatus == ABAuthorizationStatus.Restricted) {
println("access denied")
}
else if (sysAddressBookStatus == ABAuthorizationStatus.Authorized) {
println("access granted")
//獲取并遍歷所有聯(lián)系人記錄
self.readRecords();
}
}
//獲取并遍歷所有聯(lián)系人記錄
func readRecords(){
var sysContacts:NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook)
.takeRetainedValue() as NSArray
for contact in sysContacts {
//獲取姓
var lastName = ABRecordCopyValue(contact, kABPersonLastNameProperty)?
.takeRetainedValue() as! String? ?? ""
println("姓:\(lastName)")
//獲取名
var firstName = ABRecordCopyValue(contact, kABPersonFirstNameProperty)?
.takeRetainedValue() as! String? ?? ""
println("名:\(firstName)")
//昵稱
var nikeName = ABRecordCopyValue(contact, kABPersonNicknameProperty)?
.takeRetainedValue() as! String? ?? ""
println("昵稱:\(nikeName)")
//公司(組織)
var organization = ABRecordCopyValue(contact, kABPersonOrganizationProperty)?
.takeRetainedValue() as! String? ?? ""
println("公司(組織):\(organization)")
//職位
var jobTitle = ABRecordCopyValue(contact, kABPersonJobTitleProperty)?
.takeRetainedValue() as! String? ?? ""
println("職位:\(jobTitle)")
//部門
var department = ABRecordCopyValue(contact, kABPersonDepartmentProperty)?
.takeRetainedValue() as! String? ?? ""
println("部門:\(department)")
//備注
var note = ABRecordCopyValue(contact, kABPersonNoteProperty)?
.takeRetainedValue() as! String? ?? ""
println("備注:\(note)")
//獲取電話
var phoneValues:ABMutableMultiValueRef? =
ABRecordCopyValue(contact, kABPersonPhoneProperty).takeRetainedValue()
if phoneValues != nil {
println("電話:")
for i in 0 ..< ABMultiValueGetCount(phoneValues){
// 獲得標(biāo)簽名
var phoneLabel = ABMultiValueCopyLabelAtIndex(phoneValues, i).takeRetainedValue()
as CFStringRef;
// 轉(zhuǎn)為本地標(biāo)簽名(能看得懂的標(biāo)簽名撬即,比如work、home)
var localizedPhoneLabel = ABAddressBookCopyLocalizedLabel(phoneLabel)
.takeRetainedValue() as! String
var value = ABMultiValueCopyValueAtIndex(phoneValues, i)
var phone = value.takeRetainedValue() as! String
println(" \(localizedPhoneLabel):\(phone)")
}
}
//獲取Email
var emailValues:ABMutableMultiValueRef? =
ABRecordCopyValue(contact, kABPersonEmailProperty).takeRetainedValue()
if emailValues != nil {
println("Email:")
for i in 0 ..< ABMultiValueGetCount(emailValues){
// 獲得標(biāo)簽名
var label = ABMultiValueCopyLabelAtIndex(emailValues, i).takeRetainedValue()
as CFStringRef;
var localizedLabel = ABAddressBookCopyLocalizedLabel(label)
.takeRetainedValue() as! String
var value = ABMultiValueCopyValueAtIndex(emailValues, i)
var email = value.takeRetainedValue() as! String
println(" \(localizedLabel):\(email)")
}
}
//獲取地址
var addressValues:ABMutableMultiValueRef? =
ABRecordCopyValue(contact, kABPersonAddressProperty).takeRetainedValue()
if addressValues != nil {
println("地址:")
for i in 0 ..< ABMultiValueGetCount(addressValues){
// 獲得標(biāo)簽名
var label = ABMultiValueCopyLabelAtIndex(addressValues, i).takeRetainedValue()
as CFStringRef;
var localizedLabel = ABAddressBookCopyLocalizedLabel(label)
.takeRetainedValue() as! String
var value = ABMultiValueCopyValueAtIndex(addressValues, i)
var addrNSDict:NSMutableDictionary = value.takeRetainedValue()
as! NSMutableDictionary
var country:String = addrNSDict.valueForKey(kABPersonAddressCountryKey as String)
as? String ?? ""
var state:String = addrNSDict.valueForKey(kABPersonAddressStateKey asString)
as? String ?? ""
var city:String = addrNSDict.valueForKey(kABPersonAddressCityKey asString)
as? String ?? ""
var street:String = addrNSDict.valueForKey(kABPersonAddressStreetKey asString)
as? String ?? ""
var contryCode:String = addrNSDict
.valueForKey(kABPersonAddressCountryCodeKey as String) as? String?? ""
print(" \(localizedLabel): Contry:\(country) State:\(state) ")
println("City:\(city) Street:\(street) ContryCode:\(contryCode) ")
}
}
//獲取紀(jì)念日
var dateValues:ABMutableMultiValueRef? =
ABRecordCopyValue(contact, kABPersonDateProperty).takeRetainedValue()
if dateValues != nil {
println("紀(jì)念日:")
for i in 0 ..< ABMultiValueGetCount(dateValues){
// 獲得標(biāo)簽名
var label = ABMultiValueCopyLabelAtIndex(emailValues, i).takeRetainedValue()
as CFStringRef;
var localizedLabel = ABAddressBookCopyLocalizedLabel(label)
.takeRetainedValue() as! String
var value = ABMultiValueCopyValueAtIndex(dateValues, i)
var date = (value.takeRetainedValue() as? NSDate)?.description ?? ""
println(" \(localizedLabel):\(date)")
}
}
//獲取即時通訊(IM)
var imValues:ABMutableMultiValueRef? =
ABRecordCopyValue(contact, kABPersonInstantMessageProperty).takeRetainedValue()
if imValues != nil {
println("即時通訊(IM):")
for i in 0 ..< ABMultiValueGetCount(imValues){
// 獲得標(biāo)簽名
var label = ABMultiValueCopyLabelAtIndex(imValues, i).takeRetainedValue()
as CFStringRef;
var localizedLabel = ABAddressBookCopyLocalizedLabel(label)
.takeRetainedValue() as! String
var value = ABMultiValueCopyValueAtIndex(imValues, i)
var imNSDict:NSMutableDictionary = value.takeRetainedValue()
as! NSMutableDictionary
var serves:String = imNSDict
.valueForKey(kABPersonInstantMessageServiceKey as String) as? String ?? ""
var userName:String = imNSDict
.valueForKey(kABPersonInstantMessageUsernameKey as String) as? String ?? ""
println(" \(localizedLabel): Serves:\(serves) UserName:\(userName)")
}
}
}
}
Swift 獲取手機聯(lián)系人
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門就轧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來证杭,“玉大人,你說我怎么就攤上這事妒御〗夥撸” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵乎莉,是天一觀的道長送讲。 經(jīng)常有香客問我,道長惋啃,這世上最難降的妖魔是什么哼鬓? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮边灭,結(jié)果婚禮上异希,老公的妹妹穿的比我還像新娘。我一直安慰自己绒瘦,他們只是感情好称簿,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著惰帽,像睡著了一般憨降。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上善茎,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼操骡!你這毒婦竟也來了九火?” 一聲冷哼從身側(cè)響起赚窃,我...
- 正文 年R本政府宣布杉允,位于F島的核電站邑贴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏夺颤。R本人自食惡果不足惜痢缎,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望世澜。 院中可真熱鬧独旷,春花似錦、人聲如沸寥裂。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽封恰。三九已至麻养,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間诺舔,已是汗流浹背鳖昌。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- Swift 獲取聯(lián)系人信息 如今很多開發(fā)者都開始嘗試使用Swift進行開發(fā),而且獲取手機通訊錄作為開發(fā)常見的需求作...
- 孕27周的二胎媽媽阿桂端仰,在家做家務(wù)時感覺身體不對勁捶惜,去廁所一看,下身竟然出血了荔烧。 但由于出血量很少售躁,加上阿桂有妊娠...
- 體重增加,在飲食上的調(diào)整我們一般容易想到控油控糖啡直,當(dāng)然還有限酒烁涌。鹽,很少有被關(guān)注到酒觅。 鹽本身是沒有熱量的撮执,怎么會讓...
- 安裝指令 adb install (文件完整路徑,通常文件拖入命令行窗口即可) 查看所有已安裝app adb sh...