1

I'm working on a custom keyboard for a chat app. This app also has stickers and gifs. The custom keyboard can be used system wide. But the stickers and gifs only within my chat app.

Is there a way to detect the running app so I can toggle stickers/gifs on and off in the keybaord? I did some digging and found this answer on SO. But it doesn't work for me.

override public func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    if let parentViewController = self.parentViewController {
        var hostBundleID = parentViewController.valueForKey("_hostBundleID")
        println(hostBundleID)
    }
}

The viewWillAppear function doesn't even fire. Then there's the risk of getting the app rejected by Apple when you do hack-ish workarounds.

Is there a better way to do this?

Community
  • 1
  • 1
Isuru
  • 30,617
  • 60
  • 187
  • 303

1 Answers1

0

I don't think app extensions are allowed to gain any insight on the current running app or the app that invokes them.

What you could do is to embed the keyboard extension in the chat app and use the data sharing mechanism between extension and host app to detect when the host app is running (you could see the details here, in the Sharing Data with Your Containing App section).

Basically you could write a value in the shared NSUserDefaults when you open the chat app and a different value when the app is in background or closed and check the value in the keyboard extension.

Now, if the host app crashes then your NSUserDefaults value is not accurate anymore (it shows the host app running when in fact it's not) but you could implement some sort of heartbeat - write a timestamp from the host every 30s or so and check in the app extension if the timestamp is fresh enough to assume the host is still running.

It's still a (big and hairy) hack in the sense that it's convoluted and failure prone, but at least is legit from an App Store perspective.

Bogdan Farca
  • 3,856
  • 26
  • 38