iOS9 Contacts Framework get identifier from newly saved contact
Asked Answered
B

2

6

I need the identifier of a newly created contact directly after the save request. The use case: Within my app a user creates a new contact and give them some attributes (eg. name, address ...) after that he can save the contact. This scenario is working as aspected. My code looks like this:

    func createContact(uiContact: Contact, withImage image:UIImage?, completion: String -> Void)
    {    
       let contactToSave = uiContact.mapToCNContact(CNContact()) as! Cnmutablecontawctlet
       if let newImage = image
       {
          contactToSave.imageData = UIImageJPEGRepresentation(newImage, 1.0)
       }
       request           = CNSaveRequest()
       request.addContact(contactToSave, toContainerWithIdentifier: nil)
       do
       {
          try self.contactStore.executeSaveRequest(request)
          print("Successfully saved the CNContact")
          completion(contactToSave.identifier)
       }
       catch let error
       {
         print("CNContact saving faild: \(error)")
         completion(nil)
       }
  }

The Contact Object (uiContact) is just an wrapper of CNContact. In the closure completion I need to return the identifier but on this time I have no access to them, because he is creating by the system after the write process. One solution could be to fetch the newly saved CNContact with predicate

public func unifiedContactsMatchingPredicate(predicate: NSPredicate, keysToFetch keys: [CNKeyDescriptor]) throws -> [CNContact]

but this seems to me like a bit unclean because this contact could have only a name and more than one could exist. Something like a callback with the created identifier would be nice. But there isn´t. Is there a other way to solve this problem?

Behind answered 18/6, 2016 at 10:41 Comment(0)
I
2

This may be a little late but in case someone needs this.

By using the latest SDK (iOS 11), I was able to get the identifier just by:

NSError *error = nil;

saveReq = [[CNSaveRequest alloc] init];
[saveReq addContact:cnContact toContainerWithIdentifier:containerIdentifier];

if (![contactStore executeSaveRequest:saveReq error:&error]) {
    NSLog(@"Failed to save, error: %@", error.localizedDescription);
}
else
{
    if ([cnContact isKeyAvailable:CNContactIdentifierKey]) {
        NSLog(@"identifier for new contact is: %@", cnContact.identifier);
        // this works for me everytime
    } else {
        NSLog(@"CNContact identifier still isn't available after saving to address book");
    }
}
Iconoscope answered 28/11, 2017 at 16:28 Comment(0)
P
1

swift 4

This is the way to get contact id when creating contact

    do {
        try store.execute(saveRequest)
        if contactToAdd.isKeyAvailable(CNContactIdentifierKey) {
            print(contactToAdd.identifier) // here you are getting identifire
        }
    }
    catch {
        print(error)
    }
Proton answered 8/10, 2018 at 18:46 Comment(3)
Works also for swift 5Annabelle
This isnt working for me, as I find when saving a contact to a Google address book, that the identifier I saved with seems to change after it is saved. Annoyingly this means that if i add the contact to a group, it disappears when i refetch the group as it no longer exists. HOWEVER the newly created contact is now found in the address book, with a different identifier, and I could add that to the group. I am baffled as to whyHousemaster
At that time it was working for me, at this stage I was also mad for this feature. Thank you to reach here, please provide your answer once you get it.Proton

© 2022 - 2024 — McMap. All rights reserved.