我尝试使用iPhone
向ABAddressBook
地址簿添加新联系人。添加名/姓可以正常工作,但是当我尝试添加国家/地区,街道,城市等时出现错误。当我使用kABPersonAddressCountryCodeKey
或kABPersonAddressCountryKey
时出现错误。
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)
}
}
}
}
有谁知道如何解决这个问题?
答案 0 :(得分:1)
我不知道ABaddressBook但是如果你想要它在ios 9+中那么你可以设置如下CNAPtact演示 导入联系人
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!")
}
}
}
答案 1 :(得分:0)
试试这个添加地址,例如citykey
let address:ABMutableMultiValueRef = ABMultiValueCreateMutable(ABPropertyType(kABPersonAddressProperty)).takeRetainedValue()
let value = [self.CityKey]
let keys = [String(kABPersonAddressCityKey)]
let add = NSDictionary(object: value, forKey: keys)
ABMultiValueAddValueAndLabel(address,add, kABOtherLabel, nil)
ABRecordSetValue(newContact, kABPersonAddressProperty,address, &error)
print("Contact added successful? \(success)")
success = ABAddressBookSave(adbk, &error)
print("Saving addressbook successful? \(success)")