在Swift中检索ABRecordRef

时间:2014-08-15 01:02:27

标签: swift ios8 addressbookui

将RecordID保存到NSUserDefault中时遇到问题,而不是检索它并在表视图上列出。

方法中的

func peoplePickerNavigationController( peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecordRef!) {

我有以下代码来存储所选人员的身份

let contactID: ABRecordID = ABRecordGetRecordID(firstName)
if contactID != kABRecordInvalidID{
    var numberID: NSNumber = NSNumber(int: contactID)
    println(numberID.integerValue)
    contatosRecentes.append(numberID.integerValue)
}
NSUserDefaults.standardUserDefaults().setObject(contatosRecentes, forKey: "rf")

contatosRecentes

var contatosRecentes = [Int]()

我测试了代码并存储了id。问题是当我尝试使用代码获取此人的姓名时:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        var recordID: Int = contatosRecentes[indexPath.row]
        var numberID: ABRecordID = NSNumber(integer: recordID).intValue

        var errorCM: UnsafePointer<Unmanaged<CFError>?> = nil

        var addressBook = ABAddressBookCreateWithOptions(nil, errorCM)
        var addressBookRef: ABAddressBookRef? = Unmanaged<NSObject>.fromOpaque(addressBook.toOpaque()).takeUnretainedValue()

        var person = ABAddressBookGetPersonWithRecordID(addressBookRef, numberID)
        var personRef: ABRecordRef = Unmanaged<NSObject>.fromOpaque(person.toOpaque()).takeUnretainedValue() as ABRecordRef

        var personName = ABRecordCopyValue(personRef, kABPersonFirstNameProperty)
        var firstName: ABRecordRef = Unmanaged.fromOpaque(personName.toOpaque()).takeUnretainedValue() as NSString as ABRecordRef

        cell.textLabel.text = firstName as? String
        cell.imageView.image = UIImage(named: "img_profile.png")
        cell.imageView.layer.cornerRadius = 21
        cell.imageView.clipsToBounds = true;

        return cell
    }

问题是变量firstName为Empty。

ps:我在iPhone模拟器中有很多联系人

谢谢!

0 个答案:

没有答案