其他应该影响所有应用而不仅仅是地区设置的偏好也同样如此,例如:键盘设置。本地化和键盘设置(包括第三方键盘)在iOS 8.1模拟器中未被Safari、Maps和开发者应用程序正确识别。[NSLocale currentLocale]返回en_US,只提供英语和表情符号键盘。(18418630, 18512161)
如果您需要使用iOS 8.1,则某些应用程序可以使用德语布局,而其他应用程序则无法使用,例如:Safari和Maps。beta 2版中已修复了这个问题。Safari、Maps或第三方应用程序中可能不会出现其他键盘,包括第三方键盘。
NSLocale
的问题是否也导致 [[NSLocale preferredLanguages] objectAtIndex:0]
总是返回 "en"
?这似乎类似于我遇到的问题,@JeremyHuddlestonSequoia 能否帮我看一下?谢谢!https://dev59.com/al8d5IYBdhLWcg3wpzr4 - S1U这是iOS 8.1模拟器的漏洞。
我可以通过设置已使用方案中的“应用程序语言”来测试语言。
前往“产品” > “方案” > “编辑方案...”或按下cmd + Y。
在苹果公司修复之前,可以使用基于NSLocale swizzling
的以下经过检查的解决方案。
实际上,我们只需要替换错误的currentLocale
,因为在iOS8.1模拟器中它已经损坏了。
将此类别附加到项目并将其添加到.pch
文件中(不要忘记清除并重建项目)。
// NSLocale+ios8.h
// Created by Alexey Matveev on 01.11.2014.
// Copyright (c) 2014 Alexey Matveev. All rights reserved.
#if TARGET_IPHONE_SIMULATOR
// define here your locale identifier: de_DE, ru_RU, etc
#define LOCALE_IDENTIFIER @"de_DE"
@interface NSLocale (iOS8)
@end
#endif
// NSLocale+ios8.m
// Created by Alexey Matveev on 01.11.2014.
// Copyright (c) 2014 Alexey Matveev. All rights reserved.
#if TARGET_IPHONE_SIMULATOR
#import "NSLocale+ios8.h"
#import <objc/runtime.h>
@implementation NSLocale (iOS8)
+ (void)load
{
Method originalMethod = class_getClassMethod(self, @selector(currentLocale));
Method swizzledMethod = class_getClassMethod(self, @selector(swizzled_currentLocale));
method_exchangeImplementations(originalMethod, swizzledMethod);
}
+ (NSLocale*)swizzled_currentLocale
{
return [NSLocale localeWithLocaleIdentifier:LOCALE_IDENTIFIER];
}
@end
#endif
现在希望你看到的是相同的。
还有一件事,使用这种方法可以获得一个愉悦的副作用:通过类别语言环境选择的键盘始终在使用,不依赖于系统设置和添加的键盘。因此,模拟器设置重置不需要再次添加键盘。
类别方法允许一次性覆盖所有目标的语言和区域设置,而无需单独更改每个目标方案中的这些参数。
在iOS 8.1版本中,有一个已知的问题会导致模拟器中无法显示键盘。
键盘已知问题:其他键盘(包括第三方键盘)可能无法在Safari、地图或第三方应用程序中出现在模拟器中。
解决方法:键盘应该可以在日历、Spotlight、联系人和照片中进行测试。
我理解这意味着您的封闭应用程序也无法正常工作。我的键盘无法在Safari或地图中显示,但在照片搜索栏中可以正常使用。
@malex的解决方案通过交换当前语言环境并将其添加到.pch文件中,几乎对我有用。它启用了区域键盘(丹麦语),但没有使它成为默认键盘。
要使其成为默认键盘,我还必须在NSLocale的preferredLanguages方法上进行交换。
感谢@malex。
总之,最终代码如下:
@interface NSLocale (iOS8)
@end
@implementation NSLocale (iOS8)
+ (void)load
{
Method originalCurrentLocale = class_getClassMethod(self, @selector(currentLocale));
Method swizzledCurrentLocale = class_getClassMethod(self, @selector(swizzled_currentLocale));
method_exchangeImplementations(originalCurrentLocale, swizzledCurrentLocale);
Method originalPreferredLanguages = class_getClassMethod(self, @selector(preferredLanguages));
Method swizzledPreferredLanguages = class_getClassMethod(self, @selector(swizzled_preferredLanguages));
method_exchangeImplementations(originalPreferredLanguages, swizzledPreferredLanguages);
}
+ (NSLocale *)swizzled_currentLocale
{
return [NSLocale localeWithLocaleIdentifier:@"da_DK"];
}
+ (NSArray *)swizzled_preferredLanguages
{
return @[@"da"];
}
@end
NSString *locale = [[[NSProcessInfo processInfo] environment] objectForKey:@"myLocale"];
if (locale) {
NSLog(@"Using environment variable for locale");
} else{
NSLog(@"Using locale configured from settings");
}