From 8a7bcadfc993a79979fae856d4bb77ecee18e6d3 Mon Sep 17 00:00:00 2001 From: Jinya Date: Fri, 29 Oct 2021 22:50:13 +0800 Subject: [PATCH] add thread safe for in-app review & update README --- README.md | 6 ++-- .../AppStoreReviewManager.swift | 33 ++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0b53bc3..1741fd6 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,11 @@ iOS 9.0+, Swift 5.5 ## Installation -##### Swift Package Manager (Recommended) +#### Swift Package Manager (Recommended) - File > Swift Packages > Add Package Dependency - Add `https://github.com/Jinya/AppStoreReviewManager.git` -- Select "Exact Version" +- Select "Exact Version" (recommend using the latest exact version) ## How to Use @@ -23,7 +23,7 @@ iOS 9.0+, Swift 5.5 import AppStoreReviewManager // Get App Store page url String for your app -AppStoreReviewManager.appStorePageURLString(with: YOUR_APP_APP_STORE_ID) +let urlString = AppStoreReviewManager.appStorePageURLString(with: YOUR_APP_APP_STORE_ID) // Open App Store page for your app AppStoreReviewManager.openAppStorePage(with: YOUR_APP_APP_STORE_ID) diff --git a/Sources/AppStoreReviewManager/AppStoreReviewManager.swift b/Sources/AppStoreReviewManager/AppStoreReviewManager.swift index 3f23c3f..56e5361 100644 --- a/Sources/AppStoreReviewManager/AppStoreReviewManager.swift +++ b/Sources/AppStoreReviewManager/AppStoreReviewManager.swift @@ -40,23 +40,32 @@ public struct AppStoreReviewManager { /// Request StoreKit to ask the user to rate or review your app, users will submit a rating through the standardized prompt, and can write and submit a review without leaving the app. You can prompt for ratings up to three times in a 365-day period. public static func requestReviewInApp() { - if #available(iOS 14.0, *) { - let keyWindow = UIApplication.shared.connectedScenes.compactMap({ $0 as? UIWindowScene }).flatMap { $0.windows }.first { $0.isKeyWindow } - guard let windowScene = keyWindow?.windowScene else { - assertionFailure("AppStoreReviewManager can't find key window of the app!") - return + let task = { + if #available(iOS 14.0, *) { + let keyWindow = UIApplication.shared.connectedScenes.compactMap({ $0 as? UIWindowScene }).flatMap { $0.windows }.first { $0.isKeyWindow } + guard let windowScene = keyWindow?.windowScene else { + assertionFailure("AppStoreReviewManager can't find key window of the app!") + return + } + SKStoreReviewController.requestReview(in: windowScene) + } else if #available(iOS 10.3, *) { + SKStoreReviewController.requestReview() + } else { + #if DEBUG + print("This operation is not supported on systems older than iOS 10.3") + #endif } - SKStoreReviewController.requestReview(in: windowScene) - } else if #available(iOS 10.3, *) { - SKStoreReviewController.requestReview() + } + + if Thread.isMainThread { + task() } else { - #if DEBUG - print("This operation is not supported on systems older than iOS 10.3") - #endif + DispatchQueue.main.async { + task() + } } } - /// To enable a user to initiate a review as a result of an action in the UI use a deep link to the App Store page for your app with the query parameter action=write-review appended to the URL. /// - Parameter id: the App Store ID for your app, you can find the App Store ID in your app's product URL public static func requestReviewInAppStore(with id: String) {