更改Swift后,人员选取器视图中未显示ABRecordSetValue更改(可能未提交更改?)

时间:2014-08-25 01:01:30

标签: swift

我尝试使用ABRecordSetValue()更新Swift中的各种联系人属性,但下次打开它时模拟器不会显示ABPeoplePickerNavigation.presentViewController()中的更改:

  1. 点按按钮以打开联系人选择器
  2. 选择联系人
  3. 点击按钮再次打开联系人选择器
  4. 预期: 模拟器中的联系人具有我指定的新名字

    实际值: Println显示该值似乎已分配,但在视图中,名称未更改

    我假设ABAddressBookSave()会提交更改,所以我想知道是否需要提交其他内容,或者我可能需要以某种方式更新用于人员选择器视图控制器的地址簿引用以反映更改?

    首先,视图会提示要求访问联系人,然后为var adbk分配一个值:

    var adbk : ABAddressBook!
    var authDone = false
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if !self.authDone {
            self.authDone = true
            let stat = ABAddressBookGetAuthorizationStatus()
            switch stat {
            case .Denied, .Restricted:
                println("no access")
            case .Authorized, .NotDetermined:
                var err : Unmanaged<CFError>? = nil
                var adbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, &err).takeRetainedValue()
                if adbk == nil {
                    println(err)
                    return
                }
                ABAddressBookRequestAccessWithCompletion(adbk) {
                    (granted:Bool, err:CFError!) in
                    if granted {
                        self.adbk = adbk
                    } else {
                        println(err)
                    }//if
                }//ABAddressBookReqeustAccessWithCompletion
            }//case
        }//if
    }//viewDidAppear
    

    然后绑定到showPicker的按钮显示人员选择器UI:

    var person: ABRecord!
    
    @IBAction func showPicker(sender: AnyObject) {
        var picker: ABPeoplePickerNavigationController =  ABPeoplePickerNavigationController()
    
        picker.peoplePickerDelegate = self
        self.presentViewController(picker, animated: true, completion:nil)
    }//showPicker
    

    选择联系人后,我尝试更新ABRecordRef! &#34;人&#34;使用ABRecordSetValue,它返回true,而firstName prop的值也显示新值,所以我不确定出了什么问题:

    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecordRef!) -> Bool {
        self.person = person
        println(ABRecordCopyValue(person, kABPersonFirstNameProperty).takeUnretainedValue())
        var updateSucceeded = ABRecordSetValue(person,  kABPersonFirstNameProperty, "New First Name", nil)
        println("Did the update succeed? \(updateSucceeded)")
        println(ABRecordCopyValue(person, kABPersonFirstNameProperty).takeUnretainedValue())
        ABAddressBookSave(adbk, nil)
        println(ABRecordCopyValue(person, kABPersonFirstNameProperty).takeUnretainedValue())
    }//navController
    

    (缺乏)进度更新: 在Apple大肆宣誓之后,我终于在我的iPhone 5上运行了构建(必须将didSelectPerson更改为shouldContinueAfterSelectingProgram,因为前者仅为iOS8,后者为iOS 7且在iOS8中已弃用)

    结果与sim略有不同 - 虽然ABRecordSetValue和ABAddressBookSave都表示他们在手机中成功,ABRecordCopyValue刚刚保存后返回旧的,未更改的名称。在sim中,它返回更改的名称,但UI不反映该更改。

1 个答案:

答案 0 :(得分:0)

终于明白了,虽然我不得不承认我不明白为什么修复有效。

问题 - 我正在针对adbk运行ABAddressBookSave,这不起作用

解决方案 - 针对self.adbk运行ABAddressBookSave

我稍微更新了peoplePicker方法,所以这是我最新的:

//This method shouldContinueAfterSelectingPerson only works in iOS 7.0 and is deprecated in 8.0, but the 8.0 method doesn't exist in 7.0 so using this to test on my 7.1 phone
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, shouldContinueAfterSelectingPerson person: ABRecordRef!) -> Bool {
    self.adbk = peoplePicker.addressBook
    self.person = person
    println(ABRecordCopyValue(person, kABPersonFirstNameProperty).takeUnretainedValue())

    //Gets the recordId of a person such as 1,2,3,4,5.  Note this number may change if somone backs up their contacts from iCloud or something like that
    var pId = ABRecordGetRecordID(person)
    println(pId)

    updateFirstAndLastName()
    return false
}//navController

func updateFirstAndLastName(){
//Note this next line no longer works in Xcode 6.1 so I'm commenting it out and replacing error with nil
    //var error:UnsafeMutablePointer<Unmanaged<CFError>?>=nil

    var updateSucceeded = ABRecordSetValue(self.person,  kABPersonFirstNameProperty, "aa", nil)
    println("Did the update succeed? \(updateSucceeded)")
    //println("Error: \(error)")

    println(ABRecordCopyValue(self.person, kABPersonFirstNameProperty).takeUnretainedValue())

    var addressBookSave = ABAddressBookSave(self.adbk, nil)
    println("Did the addressbook save succeed? \(addressBookSave)")
    //println("Error: \(error)")

    println(ABRecordCopyValue(self.person, kABPersonFirstNameProperty).takeUnretainedValue())
}//udpateFirstAndLastName

如果我了解自我工作的细节以及为什么这个范围在我的其他班级var没有的地方工作,我会更新。