【问题标题】:Create a contact programmatically in Swift在 Swift 中以编程方式创建联系人
【发布时间】:2015-01-15 15:21:27
【问题描述】:

我正在创建一个应用程序,需要将联系人添加到该设备的通讯录中。 当我仅使用名字和姓氏将联系人添加到设备时,一切正常。但是,当我也尝试添加电话号码时,应用程序崩溃了。 谁能看到我在这里做错了什么?

提前致谢!

    let firstName = "Firstname"
    let lastName = "Lastname"
    let telephoneNumber = "1234567890"
    let notes = "This is a note"

    let person: ABRecordRef = ABPersonCreate().takeRetainedValue()

    let couldSetFirstName = ABRecordSetValue(person, kABPersonFirstNameProperty, firstName as CFTypeRef, nil)

    let couldSetLastName = ABRecordSetValue(person, kABPersonLastNameProperty, lastName as CFTypeRef, nil)
    let couldSetPhoneNumber = ABRecordSetValue(person, kABPersonPhoneProperty, telephoneNumber as CFTypeRef, nil)

    let couldSetNotes = ABRecordSetValue(person, kABPersonNoteProperty, notes, nil)

    var error: Unmanaged<CFErrorRef>? = nil

    let couldAddPerson = ABAddressBookAddRecord(inAddressBook, person, &error)

    if couldAddPerson {
        println("Added person")
    } else{
        println("Failed to add person")
        return nil
    }

    if ABAddressBookHasUnsavedChanges(inAddressBook){

        var error: Unmanaged<CFErrorRef>? = nil
        let couldSaveAddressBook = ABAddressBookSave(inAddressBook, &error)

        if couldSaveAddressBook{
            println("Saved address book")
        } else {
            println("Failed to save address book")
        }

        if couldSetFirstName && couldSetLastName {
            println("Succesfully set first and last name")
        } else{
            println("Failed to set first and last name")
        }
    }

    return person

【问题讨论】:

    标签: ios swift addressbook abaddressbook


    【解决方案1】:

    您正在传递一个字符串来设置kABPersonPhoneProperty 的值,这是不正确的。电话号码不是字符串属性;这是一个多值属性。您需要使用类似此处的代码进行设置:How to create contacts in address book in iPhone SDK?(这是 Objective-C,但应该很容易翻译。)

    NSString *phone = @"0123456789"; // the phone number to add
    
    //Phone number is a list of phone number, so create a multivalue    
    ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue, phone, kABPersonPhoneMobileLabel, NULL);
    
    ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, &anError); // set the phone number property
    

    【讨论】:

    • 嗨,杰西,感谢您的回复!我已经将剪断的内容翻译成 Swift,这几乎是相同的。不幸的是,它在第​​一行给出了一个错误。而不是“kABPersonPhoneProperty”作为参数,它要求一个“ABPropertyType”。知道如何获得这个吗?
    • @NielsRobben 是的,对不起;我已修复的原始答案中有一个错误;参数有一种要创建的多值属性,文档说电话字段是kABMultiStringPropertyType
    • 谢谢!但它仍然无法正常工作,根据 xcode 的说法,问题是 int 不能转换为“ABPropertyType”。 ABPropertyType 的返回类型不知何故是 Uint32.. 感谢您的帮助!
    • @NielsRobben 这看起来像是地址簿的 swift 标头中的错误;你需要做类似ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType))的事情。
    • 太棒了,结合在保存时请求多值属性的未保留值来修复它。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    相关资源
    最近更新 更多