类似 TipKit 的 API,可以基于事件和条件触发任意闭包或 NSNotification。
使用 Swift Package Manager 安装
dependencies: [
.package(url: "https://github.com/Dean151/RuleKit.git", from: "0.5.0"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "RuleKit", package: "RuleKit"),
]),
]
并导入它
import RuleKit
RuleKit 的核心在于:当满足一组规则时,调用一个闭包或触发一个 NSNotification!
try RuleKit.configure(storeLocation: .applicationDefault)
extension RuleKit.Event {
public static let appStarted: Self = "appStarted"
public static let entityCreated: Self = "itemCreated"
public static let promptAttempt: Self = "promptAttempt"
}
import Foundation
extension Notification.Name {
static let requestReviewPrompt = Notification.Name("RequestReviewPrompt")
}
import StoreKit
import SwiftUI
struct ContentView: View {
@Environment(\.requestReview)
private var requestReview
var body: View {
Text("Hello, World!")
.onReceive(NotificationCenter.default.publisher(for: .requestReviewPrompt)) { _ in
requestReview()
RuleKit.Event.promptAttempt.sendDonation()
}
}
}
RuleKit.setRule(
triggering: requestReviewNotification,
options: [.triggerFrequency(.monthly)],
.allOf([
.event(.promptAttempt) {
$0.donations.last?.version != .current
},
.anyOf([
.event(.entityCreated) { _ in
MyStore.shared.entityCount >= 5
},
.allOf([
.event(.appStarted) {
$0.donations.count >= 3
},
.event(.entityCreated) { _ in
MyStore.shared.entityCount >= 3
}
])
])
])
)
// Asynchronously
RuleKit.Event.appStarted.sendDonation()
// Synchronously
await RuleKit.Event.entityCreated.donate()
// Asynchronously
RuleKit.Event.appStarted.resetDonations()
// Synchronously
await RuleKit.Event.appStarted.reset()
.applicationDefault
: 将使用您应用程序的默认文档文件夹.groupContainer(identifier: String)
: 将您的事件捐赠存储在共享的 AppGroup 容器中.url(URL)
: 提供您自己的 URL。 它应该是一个目录 URL。.triggerFrequency(_)
: 节流通知捐赠或使用给定周期.dispatchQueue(_)
: 选择您希望从中发送通知的 DispatchQueue。 默认为主队列。.delay(for: _)
和 .delay(nanoseconds: _)
: 在满足特定通知后,延迟其触发。count
: 事件被捐赠的次数first
和 last
: 第一个和最后一个检索到的捐赠 (日期 + 版本)鼓励您通过打开 issues 或为错误修复、改进请求或支持提出 pull requests 来为该存储库做出贡献。 贡献建议