SwiftUI

【SwiftUI】レビュー依頼の実装

アプリにレビュー依頼のビューを実装しようと思い調べたことをまとめてみます。
まず独自のビューで依頼と実装するのはダメみたいです。
以下、参考URL
https://developer.apple.com/documentation/storekit/skstorereviewcontroller/requesting_app_store_reviews
https://qiita.com/yajima_toshu/items/4bafe5b9a6ed77e5578a

アップルの公式ドキュメントのサンプルコードは以下のようになってます。

// If the count has not yet been stored, this will return 0
var count = UserDefaults.standard.integer(forKey: UserDefaultsKeys.processCompletedCountKey)
count += 1
UserDefaults.standard.set(count, forKey: UserDefaultsKeys.processCompletedCountKey)

print("Process completed \(count) time(s)")

// Get the current bundle version for the app
let infoDictionaryKey = kCFBundleVersionKey as String
guard let currentVersion = Bundle.main.object(forInfoDictionaryKey: infoDictionaryKey) as? String
    else { fatalError("Expected to find a bundle version in the info dictionary") }

let lastVersionPromptedForReview = UserDefaults.standard.string(forKey: UserDefaultsKeys.lastVersionPromptedForReviewKey)

// Has the process been completed several times and the user has not already been prompted for this version?
if count >= 4 && currentVersion != lastVersionPromptedForReview {
    let twoSecondsFromNow = DispatchTime.now() + 2.0
    DispatchQueue.main.asyncAfter(deadline: twoSecondsFromNow) { [navigationController] in
        if navigationController?.topViewController is ProcessCompletedViewController {
            SKStoreReviewController.requestReview()
            UserDefaults.standard.set(currentVersion, forKey: UserDefaultsKeys.lastVersionPromptedForReviewKey)
        }
    }
}

iOS14以降のみ対応できれば良い場合

iOS14でSKStoreReviewController.requestReview()のメソッドはdeprecateになっているので下記コードのようにします。

if let windowScene = UIApplication.shared.windows.first?.windowScene {
          SKStoreReviewController.requestReview(in: windowScene)
}

-SwiftUI
-, ,

© 2024 swift技術ブログ Powered by AFFINGER5