问题答案
希望实现这样的功能:在应用中导入系统通讯录,依次检测各个联系人是否有地址信息,如果有的话就输出出来
看到有address framework,但是因为没有OC基础,所以不是很明白改怎么调用
求指点
标签: swift
答案
更新一下,把相关操作写成了一个函数,添加了更多可读取的属性,文件在此:https://github.com/JoeMartini/SwiftContactFunction.git
求人不若求己,自己重写了相关方法,虽然还有问题,但是现在可以实现功能了
 
// ViewController.swift
 
import UIKit
import AddressBook
import AddressBookUI
 
class ViewController: UIViewController {
   override func viewDidLoad() {
        super.viewDidLoad()
        var testSwiftContacts:Array = getSysContacts()
        if testSwiftContacts.isEmpty {
            println("no contact")
        }
        for contact in testSwiftContacts {
            println(contact["FirstName"]!+"·"+contact["LastName"]!)
        }
    }
}
 
//  SwiftContactTool.swift
 
import Foundation
import AddressBook
import AddressBookUI
 
func getSysContacts() -> [[String:String]] {
    var error:Unmanaged<CFError>?
    var addressBook: ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()
 
    let sysAddressBookStatus = ABAddressBookGetAuthorizationStatus()
 
    if sysAddressBookStatus == .Denied || sysAddressBookStatus == .NotDetermined {
        // Need to ask for authorization
        var authorizedSingal:dispatch_semaphore_t = dispatch_semaphore_create(0)
        var askAuthorization:ABAddressBookRequestAccessCompletionHandler = { success, error in
            if success {
                ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray
                dispatch_semaphore_signal(authorizedSingal)
            }
        }
        ABAddressBookRequestAccessWithCompletion(addressBook, askAuthorization)
        dispatch_semaphore_wait(authorizedSingal, DISPATCH_TIME_FOREVER)
    }
 
    return analyzeSysContacts( ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray )
}
 
func analyzeSysContacts(sysContacts:NSArray) -> [[String:String]] {
    var allContacts:Array = [[String:String]]()
    for contact in sysContacts {
        var currentContact:Dictionary = [String:String]()
        // 姓
        currentContact["FirstName"] = ABRecordCopyValue(contact, kABPersonFirstNameProperty)?.takeRetainedValue() as String? ?? ""
        // 名
        currentContact["LastName"] = ABRecordCopyValue(contact, kABPersonLastNameProperty)?.takeRetainedValue() as String? ?? ""
        // 昵称
        currentContact["Nikename"] = ABRecordCopyValue(contact, kABPersonNicknameProperty)?.takeRetainedValue() as String? ?? ""
        // 公司(组织)
        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? ?? ""
        allContacts.append(currentContact)
    }
    return allContacts
}
举一反三