-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExtensions.swift
executable file
·57 lines (49 loc) · 1.63 KB
/
Extensions.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
//
// Extensions.swift
// https://apphud.com
//
// Created by Apphud on 21/06/2019.
// Copyright © 2019 apphud. All rights reserved.
//
import Foundation
import StoreKit
extension SKProduct {
func subscriptionStatus() -> String {
if let expDate = IAPManager.shared.expirationDateFor(productIdentifier) {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .medium
let dateString = formatter.string(from: expDate)
if Date() > expDate {
return "Subscription expired: \(localizedTitle) at: \(dateString)"
} else {
return "Subscription active: \(localizedTitle) until:\(dateString)"
}
} else {
return "Subscription not purchased: \(localizedTitle)"
}
}
func localizedPrice() -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = priceLocale
let text = formatter.string(from: price)
let period = subscriptionPeriod!.unit
var periodString = ""
switch period {
case .day:
periodString = "day"
case .month:
periodString = "month"
case .week:
periodString = "week"
case .year:
periodString = "year"
default:
break
}
let unitCount = subscriptionPeriod!.numberOfUnits
let unitString = unitCount == 1 ? periodString : "\(unitCount) \(periodString)s"
return (text ?? "") + "\nper \(unitString)"
}
}