如何使用 appStoreOverlay() 推荐另一个应用程序? 代码演示

20 min read

以下是在 Swift 中使用 appStoreOverlay() 推荐另一个应用程序的示例代码:

import StoreKit

class RecommendationsViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func recommendApp(_ sender: UIButton) {
        if #available(iOS 14.0, *) {
            var components = URLComponents()
            components.scheme = "https"
            components.host = "apps.apple.com"
            components.path = "/app/{APP_ID}"

            guard let appStoreURL = components.url else {
                return
            }

            let parameters = [SKOverlay.AppConfiguration.Identifier: "{APP_STORE_IDENTIFIER}"]
            let overlay = SKOverlay(configuration: parameters)
            overlay.present(in: self.view.window!)
        } else {
            let appStoreURL = URL(string: "https://apps.apple.com/app/{APP_ID}")!
            UIApplication.shared.open(appStoreURL, options: [:], completionHandler: nil)
        }
    }

}

请注意,您需要将 {APP_ID} 替换为要推荐的应用程序的 App Store ID,并将 {APP_STORE_IDENTIFIER} 替换为您要推荐的应用程序的 App Store 应用配置标识符。 在 iOS 14 或更高版本上,此代码将显示一个覆盖视图,其中包含您要推荐的应用程序的信息和下载按钮。 在 iOS 13 或更早版本上,这将打开 App Store 应用程序并显示应用程序的页面。