检测当前的iPhone输入语言

时间:2009-09-12 11:10:15

标签: iphone keyboard

有谁知道,我可以在iPhone应用程序中获取当前的输入语言和/或键盘布局吗?我可以在输入语言被更改时收到通知吗?

6 个答案:

答案 0 :(得分:29)

在iOS 4.2及更高版本中,您可以使用UITextInputMode类来确定当前用于文本输入的主要语​​言。

[UITextInputMode currentInputMode].primaryLanguage将为您提供代表BCP 47语言代码的NSString,例如“es”,“en-US”或“fr-CA”。

当前输入模式改变时,您可以注册UITextInputCurrentInputModeDidChangeNotification进行提醒。

(您可能也对"Getting Your Apps Ready for China and other Hot New Markets" WWDC会话以及Internationalization Programming Topics感兴趣。)

答案 1 :(得分:17)

您可以通过UIResponder方法textInputMode询问当前的第一响应者(UITextField,UISearchBar等):

// assume you have instance variable pointing to search bar currently entering
UITextInputMode *inputMode = [self.searchBar textInputMode];
NSString *lang = inputMode.primaryLanguage;

答案 2 :(得分:8)

您可以将观察者添加到默认通知中心:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(inputModeDidChange:)
                                             name:@"UIKeyboardCurrentInputModeDidChangeNotification"
                                           object:nil];

此方法打印当前选定的输入语言(如"en_US""de_DE"):

- (void)inputModeDidChange:(NSNotification*)notification
{
    id obj = [notification object];
    if ([obj respondsToSelector:@selector(inputModeLastUsedPreference)]) {
        id mode = [obj performSelector:@selector(inputModeLastUsedPreference)];
        NSLog(@"mode: %@", mode);
    }
}

BUT: 以上所有内容均未记录在案,您不应在发货代码中使用它!

答案 3 :(得分:5)

来自Apple参考资料库 - “Getting the Current Language and Locale”:

NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];

答案 4 :(得分:1)

我的方式如下:

  • 将ViewController注册为UIApplicationDidBecomeActiveNotification

    的监听器

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];

  • 在applicationDidBecomeActive处理程序中,使用[NSLocale preferredLanguages]检查当前语言并相应地对其进行操作。

这种方法可以满足您的需求,并且无需使用私有API即可完全发送。

答案 5 :(得分:1)

与最佳答案一致,以下是在更改时获取键盘语言的通用解决方案。注册通知UITextInputCurrentInputModeDidChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputModeDidChange:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];

然后在inputModeDidChange

-(void)inputModeDidChange:(NSNotification *)notification {
   UIView *firstResponder = [UIView currentFirstResponder];
   UITextInputMode *currentInputMode =  firstResponder.textInputMode;
   NSString *keyboardLanguage = [currentInputMode primaryLanguage];
   NSLog(@"%@", keyboardLanguage); // e.g. en-US
}

currentFirstResponder来自UIView上的某个类别,以获取第一个响应者视图,如this所示:SO帖子:

//  UIView+Additions.h 
#import <UIKit/UIKit.h>
@interface UIView (Additions)
+ (id)currentFirstResponder;
@end

实施

//  UIView+Additions.m
#import "UIView+Additions.h"
static __weak id currentFirstResponder;
@implementation UIView (Additions)
+ (id)currentFirstResponder {
    currentFirstResponder = nil;
    // This will invoke on first responder when target is nil
    [[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:)
                                           to:nil
                                         from:nil
                                     forEvent:nil];
    return currentFirstResponder;
}

- (void)findFirstResponder:(id)sender {
    // First responder will set the static variable to itself
    currentFirstResponder = self;
}
@end