且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在运行时更改iOS模拟器的当前区域设置

更新时间:2023-09-18 23:45:28

@Desmond指出了一个有效的解决方案。直到他在这里给出一个答案来提供这些信息,让我总结一下我最后用一些代码做的事情。

解决方案,转out,与调整类方法在内部使用的方法一样简单:

The solution, turns out, is "as easy" as swizzling the methods that the class methods use internally:

beforeEach(^{
    [NSBundle ttt_overrideLanguage:@"nl"];
    [NSLocale ttt_overrideRuntimeLocale:[NSLocale localeWithLocaleIdentifier:@"nl_NL"]];
});

afterEach(^{
    [NSLocale ttt_resetRuntimeLocale];
    [NSBundle ttt_resetLanguage];
});

你看到的 ttt _... 方法上面使用NSObject,NSLocale和NSBundle上的类别来检查它是否应该使用原始方法或返回其他内容。这种方法在编写测试时可以完美运行,虽然它在技术上并不使用任何私有API,但我强烈建议您只在测试设置中使用它,而不是在提交给App Store进行审查的任何内容。

The ttt_... methods you see above use the categories on NSObject, NSLocale and NSBundle to check at runtime whether it should use the original methods or return something else. This method works flawlessly when writing your tests, and although it doesn't technically use any private API, I would strongly suggest only to use this in your test setup, not for anything you submit to the App Store for review.

在这个要点中,你会找到我添加的Objective-C类别我的应用测试目标以达到所需的行为。

In this gist you'll find the Objective-C categories I added to my app's test target to achieve the required behavior.