I am trying to embed the CNContactPickerViewController in a SwiftUI project. I managed to show the controller by using the UIViewControllerRepresentable but I am showing the controller in the presentation mode and animations do not work well.
Instead of embedding the CNContactPickerViewController, which is a UIKit component indeed, I tried to present the picker within the root view controller as shown below.
I show the CNContactPickerViewController as a piece of the root view controller by calling the SceneDelegate's present method within the SwiftUI component.
I know it isn't a good practice but works perfect and embedding it into the SwiftUI with the UIViewControllerRepresentable does not give me good results because it makes a flash effect, animation hangs while it is showing up.
I wonder the method that I've explained above is okay for Apple? Does it violate terms? Is it allowed to directly manipulate the root view controller? I have never encountered with this kind of method so I have no idea if I am doing something wrong.
Code Block Button(action: { self.isPresented = true }) { Text("Present") }.sheet(isPresented: $isPresented) { TestView() }
Instead of embedding the CNContactPickerViewController, which is a UIKit component indeed, I tried to present the picker within the root view controller as shown below.
Code Block class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? static func present() { let root = UIApplication.shared.windows.first?.rootViewController let viewController = CNContactPickerViewController() root?.present(viewController, animated: true, completion: nil) } func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { let contentView = ContentView() // Use a UIHostingController as window root view controller. if let windowScene = scene as? UIWindowScene { let window = UIWindow(windowScene: windowScene) window.rootViewController = UIHostingController(rootView: contentView) self.window = window window.makeKeyAndVisible() } } ...
I show the CNContactPickerViewController as a piece of the root view controller by calling the SceneDelegate's present method within the SwiftUI component.
Code Block Button(action: { SceneDelegate.present() }) { Text("Present") }
I know it isn't a good practice but works perfect and embedding it into the SwiftUI with the UIViewControllerRepresentable does not give me good results because it makes a flash effect, animation hangs while it is showing up.
I wonder the method that I've explained above is okay for Apple? Does it violate terms? Is it allowed to directly manipulate the root view controller? I have never encountered with this kind of method so I have no idea if I am doing something wrong.