2015-12-25 61 views
1

我正在嘗試使用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) 
      } 
     } 
    } 

} 

有誰知道我該如何解決這個問題?

回答

1

我不知道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:"[email protected]") 
    let workEmail = CNLabeledValue(label:CNLabelWork, value:"[email protected]") 
    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!") 
    } 
    } 
} 
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)")