Ani kukadiya
Ani kukadiya

Reputation: 174

How to clear Unsaved draft for contact in iOS?

  1. Open the default contact form to add a number
  2. Add a number and click on cancel to remove and open new modal for confirmation
  3. Select discard changes modal close

Now when I open the contact book then first open the modal with the last number I added in the contact book form which added from my app

here is my code:


    [self updateRecord:contact withData:contactData];

    CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];

    controller.delegate = self;

    dispatch_async(dispatch_get_main_queue(), ^{
        UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:controller];
        UIViewController *viewController = (UIViewController*)[[[[UIApplication sharedApplication] delegate] window] rootViewController];
        while (viewController.presentedViewController)
            {
                viewController = viewController.presentedViewController;
            }
        [viewController presentViewController:navigation animated:YES completion:nil];

        self->updateContactPromise = resolve;

Below is delegate method code:

    [viewController dismissViewControllerAnimated:YES completion:nil];

    if(updateContactPromise) {

        if (contact) {
            NSDictionary *contactDict = [self contactToDictionary:contact withThumbnails:true];
            updateContactPromise(contactDict);
        } else {
            
            updateContactPromise(nil);
        }

        updateContactPromise = nil;
    }
}

Upvotes: 1

Views: 3482

Answers (1)

r07aNd
r07aNd

Reputation: 1

I just saw this post and as I understood, it should be avoided that after canceling the creation of a new contact via CNContactViewController, still a unsaved draft exists when opening the contacts app on the iPhone. I found out that if a CNMutableContact is first opened in read mode and afterwards set to change via contactViewController.allowsEditing = true and contactViewController.setEditing(true, animated: true) is doing the trick to avoid unsaved drafts within the address book.

I solved it like this with Xcode 16.1 for iOS 18.1 and SWIFT 6.

struct ContactViewControllerRepresentable: UIViewControllerRepresentable {

private var contact = CNMutableContact()

func makeCoordinator() -> Coordinator {
    return Coordinator()
}

func makeUIViewController(context: Context) -> UINavigationController {
    let contactViewController = CNContactViewController(for: contact)
    contactViewController.allowsEditing = true
    contactViewController.setEditing(true, animated: true)
    contactViewController.delegate = context.coordinator
    
    self.contact.givenName = "John"
    self.contact.familyName = "Doe"
    self.contact.phoneNumbers = [CNLabeledValue(
        label: CNLabelPhoneNumberMobile,
        value: CNPhoneNumber(stringValue: "123-456-7890")
    )]
    self.contact.emailAddresses = [CNLabeledValue(
        label: CNLabelWork,
        value: "[email protected]" as NSString
    )]
    
    let navigationController = UINavigationController(rootViewController: contactViewController)
    navigationController.delegate = context.coordinator
    
    return navigationController
}

func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
    
}
}

class Coordinator: NSObject, @preconcurrency CNContactViewControllerDelegate, UINavigationControllerDelegate {
    func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
        viewController.dismiss(animated: true, completion: nil)
    }
}

Upvotes: 0

Related Questions