Xcode 14.2 模拟器中的 SKStoreProductViewController

问题描述 投票:0回答:1

最终,我想将

SKStoreProductViewController
合并到我正在从事的项目中,所以开始时我想我会重新创建 here 发布的内容。唯一的问题是,当我在模拟器(xcode 14.2)中单击我的按钮时,没有出现任何模式。可能是因为我需要在真实设备上运行它,或者可能是因为
storeViewController.loadProduct
“无声地失败”?非常感谢任何帮助!

我的虚拟应用程序:

//
//  ViewController.swift
//  SKStoreProductViewController Demo
// 

import UIKit
import StoreKit

class ViewController: UIViewController {
    let testFlightAppURL = URL(string: "https://apps.apple.com/us/app/testflight/id899247664")!
    let testFlightProductID = 899247664  

    private let button: UIButton = {
        let button = UIButton()        

        button.backgroundColor = .darkGray
        button.setTitle("Open App Store", for: .normal)       

        return button
    }()    

    override func viewDidLoad() {
        view.addSubview(button)       

        button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
    }   

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()       

        button.frame = CGRect(x: 30,
                              y: view.frame.height-150-view.safeAreaInsets.bottom,
                              width: view.frame.size.width-60,
                              height: 55)
    }
    
    @objc func didTapButton() {
        openAppStore()
    }

    func openAppStore() {
        // 2. Create an SKStoreProductViewController instance and set its delegate
        let storeViewController = SKStoreProductViewController()
        storeViewController.delegate = self

        // 3. Indicate a specific product by passing its iTunes item identifier
        let parameters = [SKStoreProductParameterITunesItemIdentifier: testFlightProductID]
        storeViewController.loadProduct(withParameters: parameters) { _, error in
            if error != nil {
                // In case there is an issue loading the product, open the URL directly
                UIApplication.shared.open(self.testFlightAppURL)
            } else {
                self.present(storeViewController, animated: true)
            }
        }
    }
}

// MARK: - SKStoreProductViewControllerDelegate
// 4. A simple implementation of SKStoreProductViewController.
// Delegate dismisses the view controller when the user completes the purchase.
extension ViewController: SKStoreProductViewControllerDelegate {
    func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) {
        viewController.dismiss(animated: true)
    }
}
ios swift iphone storekit skstorereviewcontroller
1个回答
0
投票

在真实设备上测试为我解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.