0

I'm trying to fetch the time of my app from the firebase servers using FirebaseFirestore.Timestamp() so that my users are unable to change the device time and fool the app into giving out daily credits sooner than they should get them.

To that end, I have used the following code, but this still returns the time on the device itself.

What I want to achieve is that whenever a button is pressed, getTime() gets called and prints the time as per the Firestore server, and not as per the device on which the app is installed.

My code is as below:

func getTime() {

    let time = FirebaseFirestore.Timestamp()
    print(time)
    // Prints             <FIRTimestamp: seconds=1586518212 nanoseconds=6677150>

    let RequestedDate = time.dateValue()
    print("\(RequestedDate) CHECKHERE")
    // Prints 2020-04-10 11:30:12 +0000 CHECKHERE, which is the wrong date I set using the device itself.


    let calendar = Calendar.current
    let componentsss = calendar.dateComponents([.day, .hour], from: RequestedDate)
    print(componentsss)
    // Prints the components of the device time.
}
  • "1586518212" is "2020-04-10T11:30:12+00:00 in ISO 8601". So your print is correct. Defines what's wrong exactly, and what do you expect. – Larme Apr 15 '20 at 15:42
  • @Larme thank you for letting me know, I have updated the question with more details. Sorry about that! – newKidCoderr33 Apr 15 '20 at 15:48
  • When do you want to "give out daily credits"? Is it 24-hours from when the user performs a certain action (such as starts the app for the first time)? Or is it every day at Midnight UTC? – DonMag Apr 15 '20 at 16:11
  • @DonMag thank you for replying. I want all users to get their daily credits at (or anytime after, if they don't open the app until later) midnight UTC every day. So essentially, yes, at midnight UTC everyday – newKidCoderr33 Apr 15 '20 at 16:15
  • @DonMag, In fact, the time doesn't matter, if I write my code such that anytime the date changes, the users should get their credits, that would work perfectly fine as well. Of course, I would still need the server time(which I can't seem to get) so that malicious users can't simply advance the dates on their device and get undue credits. – newKidCoderr33 Apr 15 '20 at 16:17
  • What is `FirebaseFirestore.Timestamp()`? I don't see this in the iOS SDK. Is this a custom object? – trndjc Apr 15 '20 at 16:18
  • @newKidCoderr33 - there are many approaches to this. I'd suggest searching for `swift compare device time to server time` - lots of results. Here is a link to one approach: https://stackoverflow.com/questions/31381004/swift-get-server-time – DonMag Apr 15 '20 at 16:50
  • @DonMag thank you for the link, I'll read up. – newKidCoderr33 Apr 15 '20 at 16:59

0 Answers0