There are a lot of apps that allow the user to use a functionality a certain number of times per month. And when the quota is reached that functionality is blocked till the end of the month.
I am trying to do the same thing and I seem to have run into a bit of a problem.
struct Data{
var text:String
var date:Date
}
var array : [Data] = [
Data(text: "Cars", date: Date()),
Data(text: "Bikes", date: Date()),
Data(text: "Trucks", date: Date())
]
func countForCurrentMonth(date: Date) -> Int{
let currentMonth = Calendar.current.component(.month, from: date)
print(currentMonth)
var datesArray = [Int]()
for item in array{
let month = Calendar.current.component(.month, from: item.date)
if month == currentMonth{
datesArray.append(month)
}
}
let itemCount = datesArray.count
return itemCount
}
func addItem(){
if countForCurrentMonth(date:Date()) > 120{
//functionality locked
print("subscribe to unlock")
}else{
//allowed
print("allowed")
}
}
The problem is here, the date property is compared with Date() which gives the users system date. As the users system date can be manually set, one could easily by bypass this limitation.
So my question is, how can I implement the same thing without using the system date and which doesn't require an internet connection.
One of the things that I tried was keeping a stored date on the device and every time the user opens the app it checks the system date and if it's not equal to the stored date incrementing the day count and replacing the stored date with the new date. However, this would only work if the user opens the app everyday of the month.