Custom On-Screen Keyboard For All iOS Applications .

Posted in: Blog

In a Project, there was a requirement that User can buy the Stickers/Emojis from an iOS app. That purchased stickers/emojis should be made available to all other applications in the iPhone so that they can take advantage of that.

Beginning in Xcode 6 and iOS 8, custom keyboards can be developed by using App Extensions. Developers can develop same types of creative keyboards, but now the actual task was to provide allowance to the user to use those keyboards as their default keyboard on their iOS devices – useable within any App.

Implementation:

Our iOS developer started developing a custom keyboard extension in the App. The extension then generates the KeyboardViewController.swift file. Developer then prepared KeyboardViewController.xib file manually and attach KeyboardViewController.swift with this .xib file. After that, custom keyboard was designed by using interface builder. The keyboard interface contained collection view to hold multiple images of Emojis/Stickers and next keyboard” button to toggle between the other keyboards as it is said that there are two development essentials for every custom keyboard:

1- Trust:
Your custom keyboard gives you the access to what a user types, so trust between you and your user is essential.

2- “next keyboard” key:
The accordance that lets a user switch to another keyboard is part of a keyboard’s user interface; you must provide one in your keyboard.

Challenges & Solution:

One of the challenges that we faced during the implementation was the sharing of data between Container App & custom keyboard Extension. After some R&D, it was found that iOS 8 introduced another feature named as App Groups. Following were the steps for sharing data:

  1. Selected main App target and switched App Groups to ON
  2. Created a new Container named as “group.com.mycompany.myapp
  3. Selected custom keyboard extension, then enabled App Groups in it and also created a new Container Name
  4. During reading or writing, NSUserDefaults [NSUserdefaults.standardUserDefaults()] in App/Extension were not used. Instead, we used NSUserDefaults (suiteName: “group.com.mycompany.myapp”)
    .

Another challenge was to copy the Stickers/Emojis from custom keyboard and share them with other Apps. This was done by using UIPasteboard() functionality. The code used for this was:

let pb: UIPasteboard = UIPasteboard.generalPasteboard()
 let data = NSData(data:UIImagePNGRepresentation(UIImage(named: imageName))
 pb.setData(data, forPasteboardType: "public.png")
 .

This set of code copies the images from extension and then developer was able to paste/share image with other Apps.

Leave a Reply

Your email address will not be published. Required fields are marked *