为您的 macOS 应用程序添加基于时间的试用和使用 CocoaFob 的简易授权验证。
有关使用 FastSpring 设置您自己的商店和准备应用程序的说明,请参阅我的书 在 Mac App Store 之外赚钱!
dependencies: [
.package(url: "https://github.com/CleanCocoa/TrialLicensing.git", .upToNextMajor(from: Version(3, 0, 0))),
]
通过使用此 URL 通过 Xcode 添加包依赖项:https://github.com/CleanCocoa/TrialLicensing.git
CocoaFob 会自动静态链接,无需您执行任何操作。
Trial
和 TrialLicense
库。licenseChangeBlock
和 invalidLicenseInformationBlock
的 AppLicensing
实例,用于处理更改事件。示例
import TrialLicense
let publicKey = [
"-----BEGIN DSA PUBLIC KEY-----\n",
// ...
"-----END DSA PUBLIC KEY-----\n"
].join("")
let configuration = LicenseConfiguration(appName: "AmazingApp!", publicKey: publicKey)
class MyApp: AppLicensingDelegate {
init() {
AppLicensing.setUp(
configuration: configuration,
initialTrialDuration: Days(30),
// Set up the callbacks:
licenseChangeBlock: self.licenseDidChange(licenseInfo:),
invalidLicenseInformationBlock: self.didEnterInvalidLicenseCode(name:licenseCode:),
// Get notified about initial state to unlock the app immediately:
fireInitialState: true)
}
func licenseDidChange(licenseInformation: LicenseInformation) {
switch licenseInformation {
case .onTrial(_):
// Changing back to trial may be possible if you support unregistering
// form the app (and the trial period is still good.)
return
case .registered(_):
// For example:
// displayThankYouAlert()
// unlockApp()
case .trialUp:
// For example:
// displayTrialUpAlert()
// lockApp()
// showRegisterApp()
}
}
func didEnterInvalidLicenseCode(name: String, licenseCode: String) {
// For example:
// displayInvalidLicenseAlert()
// -- or show an error label in the license window.
}
}
let myApp = MyApp()
该包声明使用了 UserDefaults
API,因为它原则上包含这些用于读取/写入信息的机制。 仅此而已。
如果您使用此功能在您的应用程序中存储实际的客户姓名和/或电子邮件地址,具体取决于您使用的许可方案,您应该检查您的应用程序是否具有 NSPrivacyCollectedDataTypes
条目,例如,值为 NSPrivacyCollectedDataTypeEmailAddress
。
LicenseInformation
揭示了您的应用程序所处的状态
enum LicenseInformation {
case registered(License)
case onTrial(TrialPeriod)
case trialUp
}
关联的类型提供额外的信息,例如在设置窗口中显示详细信息或在应用程序的标题栏中显示剩余的试用天数。
License
代表有效的名称 - 许可证代码对
struct License {
let name: String
let licenseCode: String
}
TrialPeriod
封装了试用期的持续时间。
struct TrialPeriod {
let startDate: Date
let endDate: Date
}
TrialPeriod
还提供以下便捷方法
ended() -> Bool
daysLeft() -> Days
... 其中 Days
封装了剩余时间,以便轻松转换为 TimeInterval
并公开 userFacingAmount: Int
以供显示。
版权所有 (c) 2016 Christian Tietze。 在 MIT 许可下分发。 有关详细信息,请参见 LICENSE 文件。