How to change time and timezone in iPhone simulator?

前端 未结 11 1703
礼貌的吻别
礼貌的吻别 2020-12-04 11:52

How do I change time and time zone in the iPhone simulator?

相关标签:
11条回答
  • 2020-12-04 12:21

    When changing the timezone, I found the easiest way to do it was by clicking the clock in the menubar. And then selecting "Open Date & Time Preferences" then select the tab Time Zone.

    Alternatively System Preferences -> Date and Time and select the tab Time Zone.

    Just a pointer for anyone that might not know their way around OSX.

    0 讨论(0)
  • 2020-12-04 12:30

    It would be useful if Apple provided a "Simulate date" as they provided a "Simulate Location" menu. What I do is set a breakpoint immediately after getting the date (saving it in a variable) and then modify the value. Or just modify the spot where I check for a date change and make it return true.

    0 讨论(0)
  • 2020-12-04 12:32

    I have proposed an automatic solution to the problem of changing the time that includes hacky method swizzling: https://stackoverflow.com/a/34793193/829338. I assume that should also work for changing the time zone accordingly.


    I needed to test my app automatically, which required changing the sys time. I did, what Tom suggested: happy hacky method swizzling.

    For demonstrative purposes, I only change [NSDate date] but not [NSDate dateWithTimeIntervalSince1970:].

    First your need to create your class method that serves as the new [NSDate date]. I implemented it to simply shift the time by a constant timeDifference.

    int timeDifference = 60*60*24; //shift by one day
    NSDate* (* original)(Class,SEL) = nil;
    
    +(NSDate*)date{
        NSDate* date = original([NSDate class], @selector(date));
        return [date dateByAddingTimeInterval:timeDifference];
    }
    

    So far, pretty easy. Now comes the fun part. We get the methods from both classes and exchange implementation (it worked for me in the AppDelegate, but not in my UITests class). For this you will need to import objc/runtime.h.

    Method originalMethod = class_getClassMethod([NSDate class], @selector(date));
    Method newMethod = class_getClassMethod([self class], @selector(date));
    
    //save the implementation of NSDate to use it later
    original  = (NSDate* (*)(Class,SEL)) [NSDate methodForSelector:@selector(date)];
    
    //happy swapping
    method_exchangeImplementations(originalMethod, newMethod);
    
    0 讨论(0)
  • 2020-12-04 12:33

    You can set the TZ environment variable in an Xcode Scheme to set the time zone just for that app.

    You can use UTC, PST, EST, as well as place-based timezone names such as America/Los_Angeles. It's not well documented, but I suspect any time zone name should work.

    It's not well documented, but the source is an Apple Developer Support rep on the developer forums.

    0 讨论(0)
  • 2020-12-04 12:36

    My build server is UTC and some of my unit tests needed the timezone to be PST. Using a category on NSTimeZone you can override Apple's implementation to use your code. Works for swift only projects too.

    //NSTimeZone+DefaultTimeZone.h
    #import <Foundation/Foundation.h>
    
    @interface NSTimeZone (DefaultTimeZone)
    
    +(NSTimeZone *)defaultTimeZone;
    
    @end
    
    //NSTimeZone+DefaultTimeZone.m
    #import "NSTimeZone+DefaultTimeZone.h"
    
    @implementation NSTimeZone (DefaultTimeZone)
    
    +(NSTimeZone *)defaultTimeZone
    {
        return [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-04 12:39

    After changing the system date time preferences I had to choose Hardware > Reset All Content And Settings.
    Only this worked for me in Version 10.3 (SimulatorApp-880.5 CoreSimulator-681.5.4).

    0 讨论(0)
提交回复
热议问题