How to detect in which app my custom keyboard used and show different button? E.g. in Twitter I would add @ to string I post into input field and in Reddit /r/
Asked
Active
Viewed 1,757 times
8

Schemetrical
- 5,506
- 2
- 26
- 43

Igor Barinov
- 21,820
- 10
- 28
- 33
-
2Like Schemetrical said, you can't determine the app you're in (at least, you're not *supposed* to be able to be, and probably any answer posted here would be considered a loophole and be fixed by Apple), but you *can* know that you're in a Twitter style text field, and respond appropriately, which might be better than knowing the app, since there are built in APIs to post to Twitter that can be used from any app. – Ben Pious May 25 '15 at 17:20
-
@BenPious thanks for inspirations! I appreciate any clues about "Twitter style text field" – Igor Barinov May 25 '15 at 18:39
-
2You have access in the API to an object (I forget which) which implements this protocol: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/ – Ben Pious May 25 '15 at 18:42
-
UIKeyboardTypeDefault, UIKeyboardTypeASCIICapable, UIKeyboardTypeNumbersAndPunctuation, UIKeyboardTypeURL, UIKeyboardTypeNumberPad, UIKeyboardTypePhonePad, UIKeyboardTypeNamePhonePad, UIKeyboardTypeEmailAddress, UIKeyboardTypeDecimalPad, UIKeyboardTypeTwitter, UIKeyboardTypeWebSearch, UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable – Igor Barinov May 25 '15 at 19:02
-
@BenPious how do you this is that violate developer guidelines or not? – Igor Barinov May 26 '15 at 05:22
-
@BenPious I have twitter style text field in Instagram, Slack and other apps. Eh – Igor Barinov May 30 '15 at 17:07
2 Answers
10
It is possible through following code. As you'll get bundle identifier of the app where you're using your custom keyboard.
Swift
let hostBundleID = self.parentViewController!.valueForKey("_hostBundleID")
let currentHostBundleID = String(hostBundleID)
print(currentHostBundleID);
From bundle identifier you can find app name easily.

halfer
- 19,824
- 17
- 99
- 186

Sunil Targe
- 7,251
- 5
- 49
- 80
-
is this allowed by apple?... as in would this cause an app extension to be rejected? – Will Von Ullrich Apr 20 '18 at 19:11
-
3@WillVonUllrich One of my app approved by Apple which has the same implementation. So hope it works in your case too. – Sunil Targe Apr 23 '18 at 02:41
-
-
2
3
Edit: See above. Things have changed.
This is not possible. An extension runs sandboxed and is only fed information from the API and cannot access anything else. The keyboard can only receive text context changes and activate/deactivate calls. Being able to detect an app lies outside of the extension sandbox and therefore is impossible.

Schemetrical
- 5,506
- 2
- 26
- 43
-
This is just wrong. It is possible to get the host's app bundle identified with the @halfer answer above. – Grant Oganyan Jun 02 '22 at 21:12
-
-