iOS8自定义键盘可以知道它在哪个应用程序中?

问题描述:

iOS8自定义键盘可以知道它正在写什么应用程序?我想为我的自定义键盘采取不同的行动,如果它是在Safari比邮件,照片,联系人等我都试过iOS8自定义键盘可以知道它在哪个应用程序中?

NSString *appName = [NSString stringWithString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]]; 

NSString *home_folder = NSHomeDirectory() ; 

但这些只是点到封闭的应用,这当然不会改变。

不,没有办法来检查您的键盘输入文本到什么应用程序。在UIInputViewController的文档中没有提及它,也没有提到Keyboard Extensions的更一般文档。

此外,这对用户来说会相当混乱。作为一个用户我自己,我依靠键盘在每个应用程序是相同的。否则,我学会轻松打字的难度要大得多。

+0

好点,但我期待做一些接近的时间系统键盘返回键更改其标题基于UITextField的returnKeyType属性。 – DKLA 2014-10-06 17:02:42

+0

@DKLA好主意!这对用户来说不会迷失方向。我仍然不相信有什么办法可以做到。 – Undo 2014-10-06 17:23:23

+0

键盘可以访问容器应用程序的用户默认值,并启用应用程序组。 – jgvb 2016-05-16 21:40:55

苹果的默认键盘外观是独立它运行的应用程序。键盘的外观是由键盘当前响应的对象控制。这意味着你不能强制键盘进入特定状态(只有UITextfield/UITextView可以)。

但是,您可以通过与UIReturnKeyType等各种特性合作来做出相应响应。

UIReturnKeyType

你可以做以下指定根据返回键类型不同的键盘自定义(PS下面的例子假定您的返回键按钮被称为returnButton):

if (UIReturnKeyType == UIReturnKeyDone) { 
    [returnButton setTitle:@"Done" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Done" forState:UIControlStateHighlighted]; 
} 

else if (UIReturnKeyType == UIReturnKeyGo) { 
    [returnButton setTitle:@"Go" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Go" forState:UIControlStateHighlighted]; 
} 

else if (UIReturnKeyType == UIReturnKeyJoin) { 
    [returnButton setTitle:@"Join" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Join" forState:UIControlStateHighlighted]; 
} 

else if (UIReturnKeyType == UIReturnKeyNext) { 
    [returnButton setTitle:@"Next" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Next" forState:UIControlStateHighlighted]; 
} 

else if (UIReturnKeyType == UIReturnKeySend) { 
    [returnButton setTitle:@"Send" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Send" forState:UIControlStateHighlighted]; 
} 

else if (UIReturnKeyType == UIReturnKeyYahoo) { 
    [returnButton setTitle:@"Yahoo!" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Yahoo!" forState:UIControlStateHighlighted]; 
} 

else if (UIReturnKeyType == UIReturnKeySearch) { 
    [returnButton setTitle:@"Search" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Search" forState:UIControlStateHighlighted]; 
} 

else if (UIReturnKeyType == UIReturnKeyEmergencyCall) { 
    [returnButton setTitle:@"Emergency" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Emergency" forState:UIControlStateHighlighted]; 
} 

else if (UIReturnKeyType == UIReturnKeyRoute) {  
    [returnButton setTitle:@"Route" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Route" forState:UIControlStateHighlighted]; 
} 

else if (UIReturnKeyType == UIReturnKeyGoogle) { 
    [returnButton setTitle:@"Google" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Google" forState:UIControlStateHighlighted]; 
} 

else { 
    [returnButton setTitle:@"Return" forState:UIControlStateNormal]; 
    [returnButton setTitle:@"Return" forState:UIControlStateHighlighted]; 
} 

UIKeyboardAppearance

请记住,您还可以响应其他一些事情,例如UITextField/UITextView是否使用浅色或深色键盘。

if (UIKeyboardAppearance == UIKeyboardAppearanceDark) { 
    //Customize your keyboard's dark mode 
} else { 
    //Customize your keyboard's light mode 
} 

欲了解更多信息,我建议阅读UITextInputTraits。我很高兴你正在考虑处理这一切!大多数第三方键盘缺乏一些最基本的功能,这是不幸的。