-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathLicenseChecker.swift
75 lines (60 loc) · 2.14 KB
/
LicenseChecker.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// LicenseChecker.swift
// XCreds
//
// Created by Timothy Perfitt on 3/28/23.
//
import Cocoa
class LicenseChecker: NSObject {
enum LicenseState {
case valid(Int)
case invalid
case trial(Int)
case trialExpired
case expired
}
func currentLicenseState() -> LicenseState {
let trialDays = 14
if UserDefaults.standard.value(forKey: "tts") == nil {
TCSLogErrorWithMark("setting trial date")
UserDefaults.standard.setValue(Date(), forKey: "tts")
}
let firstLaunchDate = UserDefaults.standard.value(forKey: "tts") as? Date
var trialState = LicenseState.trialExpired
if let firstLaunchDate = firstLaunchDate {
let secondsPassed = Date().timeIntervalSince(firstLaunchDate)
let trialDaysLeft=trialDays-(Int(secondsPassed)/(24*60*60));
TCSLogWithMark("trial days: \(secondsPassed)")
if secondsPassed<Double(24*60*60*trialDays) {
trialState = .trial(trialDaysLeft)
}
}
else {
TCSLogErrorWithMark("did not get first launch date")
}
let check = TCSLicenseCheck()
let status = check.checkLicenseStatus("com.twocanoes.xcreds", withExtension: "")
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withFractionalSeconds, .withFullDate]
switch status {
case .valid:
TCSLogWithMark("valid license")
if let dateExpiredString = check.license.dateExpired,let dateExpires = dateFormatter.date(from:dateExpiredString ){
return .valid(Int(dateExpires.timeIntervalSinceNow))
}
return .valid(0)
case .expired:
TCSLogErrorWithMark("expired license")
return trialState
case .invalid:
TCSLogErrorWithMark("license invalid")
return LicenseState.invalid
case .unset:
TCSLogErrorWithMark("No License")
return trialState
default:
TCSLogErrorWithMark("invalid license")
return trialState
}
}
}