Hi,
In my app, a button should be pressed and then the App Store product page of a specific app should be displayed within the application. My solution creates a UIViewControllerView of the Apple product page with .sheet.
Unfortunately, an empty view appears first and a little later the App Store product page is displayed too and the empty view is in the background. If you then close the App Store page, you also have to close the empty view until you are back in the ContentView.
Here are a screenshot.
How can I prevent the blank page or what do I have to adjust in my code so that the page does not appear?
Here is my code.
import SwiftUI
struct ContentView: View {
@State private var showAppStore = false
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
.onTapGesture {
self.showAppStore.toggle()
}
.sheet(isPresented: $showAppStore) {
AppStoreProductView()
}
Text("Hello, world!")
}
.padding()
}
}
import SwiftUI
struct AppStoreProductView: UIViewControllerRepresentable {
typealias UIViewControllerType = ViewController
func makeUIViewController(context: Context) -> ViewController {
return ViewController()
}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {
uiViewController.openAppStore()
}
}
import StoreKit
class ViewController: UIViewController {
let appURL = URL(string: "https://itunes.apple.com/app/idxxxxxxxx")!
let productID = xxxxxxxx
func openAppStore() {
let appStoreVC = SKStoreProductViewController()
appStoreVC.delegate = self
let parametersDictionary = [SKStoreProductParameterITunesItemIdentifier: productID]
appStoreVC.loadProduct(withParameters: parametersDictionary) { loaded, error in
guard error == nil, loaded else {
return
}
self.present(appStoreVC, animated: true)
}
}
}
extension ViewController: SKStoreProductViewControllerDelegate {
func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) {
viewController.dismiss(animated: true)
}
}
Many thanks in advance for any help to eliminate the blank view. Sven