Sunday, November 6, 2022

[FIXED] How to open Edit Contact UI for existing contact in swift using Contact UI

Issue

I am working on iOS application in which I want to Add new contact in Phonebook using Contact UI. I know how to open Contact UI for New Contact but I am facing difficulty in opening the existing Contact in Contact UI for Edit.

I am opening New Contact UI as

    let contact = CNMutableContact()
    
    let localizedLabelString = CNLabeledValue<NSString>.localizedString(forLabel: CNLabelPhoneNumberMobile)
    let phoneNumber = CNPhoneNumber(stringValue: contacTNumber )
    let labeledPhoneNumber = CNLabeledValue(label: localizedLabelString, value: phoneNumber)
    contact.phoneNumbers.append(labeledPhoneNumber)
    
    let controller = CNContactViewController(forNewContact: contact)
    controller.delegate = self
    controller.displayedPropertyKeys = [CNContactPhoneNumbersKey, CNContactGivenNameKey]
    let nav = UINavigationController(rootViewController: controller)
    self.present(nav, animated: true, completion: nil)

Below is the Edit contact UI that I want to open.

enter image description here


Solution

There are different constructors for the view controller depending on whether the contact is new, existing or unknown.

init(for: CNContact)
Initializes a view controller for an existing contact.

init(forUnknownContact: CNContact)
Initializes a view controller for an unknown contact.

init(forNewContact: CNContact?)
Initializes a view controller for a new contact.

You code is using init(forNewContact:). Try using init(for:)



Answered By - Dale
Answer Checked By - Willingham (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.