8

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/

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
Igor Barinov
  • 21,820
  • 10
  • 28
  • 33
  • 2
    Like 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
  • 2
    You 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 Answers2

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
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