首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >要在设置中启用当前的键盘吗?

要在设置中启用当前的键盘吗?
EN

Stack Overflow用户
提问于 2013-03-15 07:59:49
回答 2查看 736关注 0票数 4

有没有办法确定在设置中启用了哪些键盘?

只有在启用了中文键盘的情况下才能共享到新浪微博,所以我只想在有中文键盘的情况下显示“新浪微博”动作单按钮。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-15 09:04:28

多亏了Guy的评论,有了更好的方法来做到这一点。我已经更新了我自己的代码以使用以下代码:

代码语言:javascript
运行
复制
NSArray *keyboards = [UITextInputMode activeInputModes];
for (UITextInputMode *mode in keyboards) {
    NSString *name = mode.primaryLanguage;
    if ([name hasPrefix:@"zh-Han"]) {
        // One of the Chinese keyboards is installed
        break;
    }
}

Swift:(注意:由于UITextInputMode activeInputModes的错误声明,在iOS 9.x下已损坏。有关解决方法,请参阅this answer。)

代码语言:javascript
运行
复制
let keyboards = UITextInputMode.activeInputModes()
for var mode in keyboards  {
    var primary = mode.primaryLanguage
    if let lang = primary {
        if lang.hasPrefix("zh") {
            // One of the Chinese keyboards is installed
            break
        }
    }
}

老方法:

我不知道app Store应用程序中是否允许这样做,但你可以这样做:

代码语言:javascript
运行
复制
NSArray *keyboards = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleKeyboards"];
for (NSString *keyboard in keyboards) {
    if ([keyboard hasPrefix:@"zh_Han"]) {
        // One of the Chinese keyboards is installed
    }
}

如果用户的区域设置只有一个默认键盘,则可能没有AppleKeyboards键的输入。在这种情况下,您可能需要检查用户的区域设置。如果语言环境是中国的,那么你应该假设他们有一个中文键盘。

票数 4
EN

Stack Overflow用户

发布于 2016-12-14 21:04:37

这段代码对我从父应用本身的设备设置中识别键盘扩展是否激活非常有帮助:

代码语言:javascript
运行
复制
    //Put below function in app delegate...

    public func isKeyboardExtensionEnabled() -> Bool {
        guard let appBundleIdentifier = NSBundle.mainBundle().bundleIdentifier else {
            fatalError("isKeyboardExtensionEnabled(): Cannot retrieve bundle identifier.")
        }

        guard let keyboards = NSUserDefaults.standardUserDefaults().dictionaryRepresentation()["AppleKeyboards"] as? [String] else {
            // There is no key `AppleKeyboards` in NSUserDefaults. That happens sometimes.
            return false
        }

        let keyboardExtensionBundleIdentifierPrefix = appBundleIdentifier + "."
        for keyboard in keyboards {
            if keyboard.hasPrefix(keyboardExtensionBundleIdentifierPrefix) {
                return true
            }
        }

        return false
    }


    // Call it from below delegate method to identify...

    func applicationWillEnterForeground(_ application: UIApplication) {

            if(isKeyboardExtensionEnabled()){            
                showAlert(message: "Hurrey! My-Keyboard is activated");
            }
            else{
                showAlert(message: "Please activate My-Keyboard!");
            }

        }

    func applicationDidBecomeActive(_ application: UIApplication) {

            if(isKeyboardExtensionEnabled()){            
                showAlert(message: "Hurrey! My-Keyboard is activated");
            }
            else{
                showAlert(message: "Please activate My-Keyboard!");
            }
        }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15422433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档