0

I would like to open the app store with my app inside my swiftui app, e.g with sheet.

I found this article Link:

Here are my code for SKStoreProductWithClosure.swift class.

import StoreKit

class ViewController: UIViewController {
    
    let appURL = URL(string: "https://itunes.apple.com/app/idxxxxxxxx")!
    let productID = xxxxxxxxx

    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)
    }
}

Here are my current ContentView.

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) {
                    // Here I need the openAppStore() function
                }
        }
        .padding()
        
    }
}

#Preview {
    ContentView()
}

But I have big trouble to implement the class into my ContentView. I had some solution but then sheet comes up twice (one blank and after the app store sheet)

May I ask you, give me some help or hints.

Many thanks in advance. Sven

I expect to click on the image and the sheet with the app store content of my app comes up.

1
  • "I had some solution" Show it, please. We need to see how and why you are incorporating a UIViewController (UIKit) into a SwiftUI app. Commented Dec 4, 2023 at 23:51

1 Answer 1

-1

use uiviewcontrollerrepresentable to connect viewcontroller and view

struct MyView: UIViewControllerRepresentable { 

    func makeUIViewController(context: Context) -> MyViewController {
        return ViewController()  
    }

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

and you can call openAppStore() inside viewdidappear

Sign up to request clarification or add additional context in comments.

4 Comments

Many thanks. I used your code but I have other issues. See my post above yours. I'm not familiar with the technique to answer again on my question. The order is not correct. Sorry for this.
@piro299 you don't have to use .sheet function of swiftui. since the delegate opens the sheet for you. Try to use ZStack and tweak with some boolean flags to show the view and make sure the viewdidappear/openappatore() is called when the flag turns true.
Hi, I tried it. I have created a variable let appStoreProductView = ViewController() and then call it in the onTabGesture() function - appStoreProductView.openAppStore(). But nothing came up. I have this message in xcode "Attempt to present <SKStoreProductViewController: 0x15b009400> on <UpdateAppFromPlist.ViewController: 0x159805e50> (from <UpdateAppFromPlist.ViewController: 0x159805e50>) whose view is not in the window hierarchy."
I found a solution. See my post in the apple developer forum.developer.apple.com/forums/thread/742714?page=1#773999022

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.