IAppRater

用于 macOS 和 iOS 应用的微型库,用于显示“为我的应用评分”提醒。

支持的操作系统

iOS(?), MacOS(>=12)

主要特性

如何使用,SwiftUI 示例

//初始化自定义 AppDelegate

import SwiftUI

@main
struct FocusitoApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        MainView()
    }
}

//在 AppDelegate 的 DidFinishLaunching 中初始化 AppRater

class AppDelegate: NSObject, NSApplicationDelegate {
    static var shared: AppDelegate!
    
    var appRater: IAppRater!
    
    func applicationDidFinishLaunching(_ notification: Notification) {
        AppDelegate.shared = self
        
        // if we do not need custom display alert logic
        //appRater = IAppRater(minLaunches: 10,
        //                     minDays: 15,
        //                     rateWndType: .standardAlert
        //)
        
        // if we need custom display logic
        appRater = IAppRater(minLaunches: 10,
                             minDays: 15,
                             other: { _ in Stats.shared.sessions.count > 100 },
                             rateWndType: .standardAlert
        )
        
        // if we need to show request on app run:
        // appRater.requestIfNeeded()
        // and other code does not needed
    }
}

// 我们可以定位按钮以打开某个面板

struct MainView : View {
    var body: some View {
        VStack {
            MyMainViewBody()
            
            if AppDelegate.shared.appRater.isNeededToRate() {
                Button("Rate my app") { model.showBottomPanel.toggle() }
            } 
        }
    }
}

// 我们可以定位按钮以显示“为我的应用评分”提醒

struct MainView : View {
    var body: some View {
        VStack {
            MyMainViewBody()
            
            if AppDelegate.shared.appRater.isNeededToRate() {
                Button("Rate my app") { AppDelegate.shared.appRater.requestIfNeeded() }
            } 
        }
    }
}

// 我们可以打开标准的操作系统提醒

// 或者我们可以调用 AppStore 的“为我的应用评分”提醒

appRater = IAppRater(...., rateWndType: .appStoreWnd(appId: "1473808464") )

用于显示带有“为我的应用评分”按钮的面板的额外自定义逻辑

appRater = IAppRater(minLaunches: 2,
                     minDays: 2,
                     other: { me in // 0
                        (MainViewModel.shared.appState == .Idle || MainViewModel.shared.appState == .Paused) && // 1
                            Stats.shared.sessions.map{ $0.duration }.sum() > TimeInterval(hrs: 5) && // 2
                            me.lastReviewDate == nil // 3
                     },
                     rateWndType: .appStoreWnd(appId: "1473808464")
                    )

如果您需要调试 "isNeededToRate()" 值 - 您可以设置输入参数