Ios 在Swift中以编程方式创建联系人

Ios 在Swift中以编程方式创建联系人,ios,swift,addressbook,abaddressbook,Ios,Swift,Addressbook,Abaddressbook,我正在创建一个应用程序,需要将联系人添加到该设备的通讯簿中。 当我只使用名字和姓氏将联系人添加到设备时,一切正常。但是,当我尝试添加电话号码时,应用程序崩溃。 有人能看出我做错了什么吗 提前谢谢 let firstName = "Firstname" let lastName = "Lastname" let telephoneNumber = "1234567890" let notes = "This is a note" let person: A

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

提前谢谢

    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
让firstName=“firstName”
让lastName=“lastName”
让电话号码=“1234567890”
let notes=“这是一张便条”
let person:ABRecordRef=ABPersonCreate().takeRetainedValue()
让couldSetFirstName=ABRecordSetValue(person,kabbersonfirstnameproperty,firstName为CFTypeRef,nil)
让couldSetLastName=ABRecordSetValue(person,kabpersonlastname属性,lastName为CFTypeRef,nil)
让couldSetPhoneNumber=ABRecordSetValue(person,kABPersonPhoneProperty,电话号码为CFTypeRef,nil)
let couldSetNotes=ABRecordSetValue(person,kABPersonNoteProperty,notes,nil)
var错误:非托管?=无
let couldAddPerson=ABAddressBookAddRecord(inAddressBook、person和error)
如果可以添加个人{
println(“新增人员”)
}否则{
println(“未能添加人员”)
归零
}
如果ABAddressBook有未保存的更改(inAddressBook){
变量错误:非托管?=nil
让couldSaveAddressBook=ABAddressBookSave(inAddressBook,&错误)
如果可以保存地址簿{
println(“保存的通讯簿”)
}否则{
println(“保存通讯簿失败”)
}
如果couldSetFirstName&&couldSetLastName{
println(“成功设置名字和姓氏”)
}否则{
println(“未能设置名字和姓氏”)
}
}
返回人

您正在传递一个字符串来设置
KabbersonPhoneProperty
的值,该值不正确。电话号码不是字符串属性;这是一个多值属性。您需要使用下面的代码来设置它:(这是Objective-C,但应该很容易翻译)


嗨,杰西,谢谢你的回复!我已经把那个剪下来的翻译成了斯威夫特,几乎一模一样。不幸的是,它在第一行给出了一个错误。它不是将“KabbersonPhoneProperty”作为参数,而是请求一个“ABPropertyType”。你知道怎么弄到这个吗?@NielsRobben是的,对不起;原来的答案中有一个错误,我已经修复了;这里的参数是要创建的多值属性,文档称电话字段为
kABMultiStringPropertyType
。谢谢!但是它仍然不起作用,问题是,根据xcode,int不能转换为'ABPropertyType'。ABPropertyType的返回类型为Uint32。。感谢你的帮助@Nielsroben这看起来像是地址簿swift标题中的一个bug;您需要执行类似于
ABMultiValueCreateMutable(ABPropertyType(kabmultipstringpropertyType))
的操作。这将与保存时请求多值属性的未保留值相结合来修复它。谢谢你的帮助!
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