Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change system date time before launch test [XCTest, swift]

I have no idea How to change system date time before launch test on real devices or simulator. Could you help me?

like image 951
KeylorNavas Avatar asked Nov 15 '25 22:11

KeylorNavas


1 Answers

I don't think you can actually change the system time. What you can do is provide your own class that returns dates and use it everywhere in place of the Date class. In your custom class you will be able to choose to return either your test time or system time, depending on if you are testing or not.

If you do this, your in-app code would read something like let now = CustomDate() instead of let now = Date().

This question does already have an answer for Objective-C here: Set iPhone date/time within app for testing purposes?.

like image 196
N Brown Avatar answered Nov 18 '25 20:11

N Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!