Reputation: 174
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
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