Post

Replies

Boosts

Views

Activity

Reply to Swiftui - SKStoreProductViewController inside .sheet action open 2 views - why?
I also ask the question in stackoverflow and got the hint not to use .sheet. I should call the openAppStore function directly. Here is my try but I got the following message and no App Store page comes up. import SwiftUI struct ContentView: View { let appStoreProductView = ViewController() var body: some View { VStack { Image(systemName: "apple.logo") .imageScale(.large) .onTapGesture { appStoreProductView.openAppStore() } Text("Hello, world!") } .padding() } } Xcode message: Attempt to present <SKStoreProductViewController: 0x10900c200> on <UpdateAppFromPlist.ViewController: 0x104d069c0> (from <UpdateAppFromPlist.ViewController: 0x104d069c0>) whose view is not in the window hierarchy. I hope, this could be the right direction. Then I don't need struct AppStoreProductView: UIViewControllerRepresentable to create the view. Many thanks in advance for any kind of help. Sven
Dec ’23
Reply to Swiftui - SKStoreProductViewController inside .sheet action open 2 views - why?
I found a solution. class AppStoreVC: 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) { _, error in if error != nil { guard let url = self.appURL else { return } UIApplication.shared.open(url) } else { let scene = UIApplication.shared.connectedScenes.first { $0.activationState == .foregroundActive } if let windowScene = scene as? UIWindowScene { windowScene.keyWindow?.rootViewController?.present(appStoreVC, animated: true, completion: nil) } } } } } extension AppStoreVC: SKStoreProductViewControllerDelegate { func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) { viewController.dismiss(animated: true) } } struct ContentView: View { let appStoreVC = AppStoreVC() var body: some View { VStack { Image(systemName: "apple.logo") .onTapGesture { appStoreVC.openAppStore() } Text("Open App Store") } .padding() } }
Dec ’23