gpt4 book ai didi

ios - 如何在地址簿中添加地址属性?

转载 作者:行者123 更新时间:2023-11-28 06:52:45 25 4
gpt4 key购买 nike

我正在尝试使用 ABAddressBook 将新联系人添加到 iPhone 地址簿。添加名字/姓氏工作正常,但是当我尝试添加国家、街道、城市等时出现错误。使用 kABPersonAddressCountryCodeKeykABPersonAddressCountryKey 时出现错误>。

func addUserToAddressBook(){
let stat = ABAddressBookGetAuthorizationStatus()
switch stat {
case .Denied, .Restricted:
print("no access to addressbook")
case .Authorized, .NotDetermined:
var err : Unmanaged<CFError>? = nil
let adbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, &err).takeRetainedValue()
if adbk == nil {
print(err)
return
}
ABAddressBookRequestAccessWithCompletion(adbk) {
(granted:Bool, err:CFError!) in
if granted {
let newContact:ABRecordRef! = ABPersonCreate().takeRetainedValue()
var success:Bool = false

//Updated to work in Xcode 6.1
var error: Unmanaged<CFErrorRef>? = nil

success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, self.userFirstName. , &error)
print("Setting first name was successful? \(success)")
success = ABRecordSetValue(newContact, kABPersonLastNameProperty, self.user.LastName, &error)
print("Setting last name was successful? \(success)")

// Address??????
success = ABRecordSetValue(newContact, kABPersonAddressCountryCodeKey, self.user.CountryCode, &error) // self.user.CountryCode = "CN" - Receive an error

success = ABAddressBookAddRecord(adbk, newContact, &error)
print("Contact added successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Saving addressbook successful? \(success)")

} else {
print(err)
}
}
}

}

有谁知道我该如何解决这个问题?

最佳答案

我不知道 ABAddressBook,但如果你想在 iOS 9+ 中使用它,那么你可以使用 CNContact 按照演示进行设置 导入联系人

func createContcat()
{
if #available(iOS 9.0, *) {

let contact = CNMutableContact()

contact.imageData = NSData() // The profile picture as a NSData object

contact.givenName = "Jack"
contact.familyName = "test"

let homeEmail = CNLabeledValue(label:CNLabelHome, value:"jaydeep@example.com")
let workEmail = CNLabeledValue(label:CNLabelWork, value:"j.appleseed@icloud.com")
contact.emailAddresses = [homeEmail, workEmail]

contact.phoneNumbers = [CNLabeledValue(
label:CNLabelPhoneNumberiPhone,
value:CNPhoneNumber(stringValue:"12346579"))]

let homeAddress = CNMutablePostalAddress()
homeAddress.street = "1 Infinite Loop"
homeAddress.city = "Test"
homeAddress.state = "Guj"
homeAddress.postalCode = "12345"
homeAddress.country = "Country"
contact.postalAddresses = [CNLabeledValue(label:CNLabelHome, value:homeAddress)]

let birthday = NSDateComponents()
birthday.day = 14
birthday.month = 2
birthday.year = 1991 // You can omit the year value for a yearless birthday
contact.birthday = birthday

// Saving the newly created contact
let store = CNContactStore()
let saveRequest = CNSaveRequest()
saveRequest.addContact(contact, toContainerWithIdentifier:nil)

do {
try store.executeSaveRequest(saveRequest)
} catch {
print("Something went wrong!")
}
}
}

关于ios - 如何在地址簿中添加地址属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34461923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com