2

I want to get the current UTC timestamp from Firebase. I was getting UTC timestamp from device but I changed this approach so that user does not use phone time & use Firebase time which can't be changed.

I used below code but it does not return me timestamp.

 print("server value \(FIRServerValue.timestamp())") 

It gives me value as [AnyHashable(".sv"): timestamp]

I also tried this

 let objectToSave = ["date": [".sv": "timestamp"]]
        print("timestamp \(objectToSave)")

Now I have two questions:

  • Is it good approach to use Firebase time so that user can't change time of device ?
  • How to get timestamp's value ?
rmaddy
  • 314,917
  • 42
  • 532
  • 579
TechChain
  • 8,404
  • 29
  • 103
  • 228
  • This is what you want https://stackoverflow.com/questions/29243060/trying-to-convert-firebase-timestamp-to-nsdate-in-swift – DoesData Dec 01 '17 at 12:38

1 Answers1

0

So, I will separate the answer in two parts to match your two questions:

• Is it a good approach ?

It depends on what your app does and how you want it to work. For instance, for a game where you may have a feature where a player should wait one hour before being able to play again, it is actually the only solution to beat a user "cheating" by forwarding its device's clock. However, if your app is a messaging app or anything else, your can have two approaches: (1) you don't care what is the real time because your code can react to any situation (dates in the future or something like that), (2) you think it will make you app runs more smoothly because you prevent edge cases to happen in the first place, and in this case, you should get the real time.

Another example you could consider are Apple own apps. I played a lot with my MacBook's internal time to test with Xcode and the iOS simulator my own app's events in the future or past. I realised that Xcode and even Finder are based on the time the Mac is telling them. So I end up with files and Xcode logs created in two months.

• How to get timestamp value ?

It seems you're not accessing the FIRServerValue.timeStamp() properly. Firebase documentation says it returns like that: class func timestamp() -> [AnyHashable : Any]. To manipulate the result you're getting from Firebase, I would suggest you check these two SO similar posts that treat this matter in greater detail:

Trying to convert Firebase timestamp to NSDate in Swift

Swift 4: Firebase Timestamp

standousset
  • 1,092
  • 1
  • 10
  • 25