NSString
object, at the text insertion point of the current text input object.UIKeyboardType
trait of the current text input object. With the insertion point in the To: field in Mail, for example, the system keyboard period key changes: When you press and hold that key, you can pick from among a set of top-level domain suffixes. Design your custom keyboard with keyboard type traits in mind.secureTextEntry
property being set to YES
true
and is distinguished by presenting typed characters as dots.application:shouldAllowExtensionPointIdentifier:
method from the UIApplicationDelegate
protocol (returning a value of NO
false
), and thereby always uses the system keyboard.UIInputViewController
object, it cannot select text. Text selection is under the control of the app that is using the keyboard. If that app provides an editing menu interface (such as for Cut, Copy, and Paste), the keyboard has no access to it. A custom keyboard cannot offer inline autocorrection controls near the insertion point.UIInputViewController
class, which serves as your keyboard’s primary view controller. The template also includes a basic implementation of the “next keyboard” key, which calls the advanceToNextInputMode
method of the UIInputViewController
class. Add objects such as views, controls, and gesture recognizers to the input view controller’s primary view (in its inputView
property), as suggested in Figure 8-1. As with other app extensions, there is no window in the target, and, therefore, no root view controller per se.Info.plist
file comes preconfigured with the minimal values needed for a keyboard. See the NSExtensionAttributes
dictionary key in the keyboard target’s Info.plist
file. The keys for configuring a keyboard are described in Configuring the Info.plist file for a Custom Keyboard.RequestsOpenAccess
Boolean key in the Info.plist
file to YES
true
. Doing this expands the keyboard’s sandbox, as described in Designing for User Trust.UIKeyInput
protocol methods insertText:
and deleteBackward
. Call these methods on the input view controller’s textDocumentProxy
property, which represents the current text input object and which conforms to the UITextDocumentProxy
protocol. For example:[self.textDocumentProxy insertText:@'hello ']; // Inserts the string 'hello ' at the insertion point
[self.textDocumentProxy deleteBackward]; // Deletes the character to the left of the insertion point
[self.textDocumentProxy insertText:@'n']; // In a text view, inserts a newline character at the insertion point
deleteBackward
method, obtain the textual context near the insertion point from the documentContextBeforeInput
property of the textDocumentProxy
property, as follows:NSString *precedingContext = self.textDocumentProxy.documentContextBeforeInput;
adjustTextPositionByCharacterOffset:
method of the UITextDocumentProxy
protocol. For example, to delete forward by one character, use code similar to this:- (void) deleteForward {
[self.textDocumentProxy adjustTextPositionByCharacterOffset: 1];
[self.textDocumentProxy deleteBackward];
}
UITextInputDelegate
protocol.UIKeyboardType
property. For each trait you support, change the contents of your primary view accordingly.primaryLanguage
property of the UIInputViewController
class.RequestsOpenAccess
key) has access to a basic autocorrection lexicon through the UILexicon
class. Make use of this class, along with a lexicon of your own design, to provide suggestions and autocorrections as users are entering text. The UILexicon
object contains words from various sources, including:CGFloat _expandedHeight = 500;
NSLayoutConstraint *_heightConstraint = [NSLayoutConstraint constraintWithItem: self.view attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 0.0 constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];
Open access | Capabilities and restrictions | Privacy considerations |
---|---|---|
Off (default) |
|
|
On |
|
|
Capability | Example user benefit | Developer responsibility |
---|---|---|
Shared container with containing app | Management UI for keyboard’s autocorrect lexicon | Consider the autocorrect lexicon to be private user data. Do not send it to your servers for any purpose that is not obvious to the user. |
Sending keystroke data to your server | Enhanced touch-event processing and input prediction via developer’s computing resources | Do not store received keystroke or voice data except to provide services that are obvious to the user. |
Dynamic autocorrect lexicon based on network supplied data | Names of people, places, and current events in the news added to autocorrection lexicon | Do not associate the user’s identity with their use of trending or other network-based information, for any reason that is not obvious to the user. |
Address Book access | Names, places, and phone numbers relevant to the user added to autocorrection lexicon | Do not use Address Book data for any purpose that is not obvious to the user. |
Location Services access | Nearby place names added to autocorrection lexicon | Do not use Location Services in the background. Do not send location data to your servers for any purpose that is not obvious to the user. |
needsInputModeSwitchKey
property on the input view controller. If true, your keyboard should include this key.advanceToNextInputMode
method of the UIInputViewController
class. The system picks the appropriate “next” keyboard from the list of user-enabled keyboards; there is no API to obtain a list of enabled keyboards or for picking a particular keyboard to switch to.advanceToNextInputMode
method as the action of its Next Keyboard key. For best user experience, place your “next keyboard” key close to the same screen location as the system keyboard’s Globe key.Info.plist
file, located in the app’s Supporting Files folder. The property list editor opens, showing the contents of the file.Bundle display name
and when the name autocompletes, press Return.Info.plist
files for the keyboard and its containing app. iOS user interface text | Info.plist key |
---|---|
| Bundle display name in containing app’s Info.plist file |
| Bundle display name in custom keyboard target’s Info.plist file |
viewDidLoad
method.viewDidLoad
breakpoint by again invoking the keyboard).Info.plist
file) keys that are specific to a custom keyboard let you statically declare the salient characteristics of your keyboard, including its primary language and whether it requires open access.Info.plist
file in the Project navigator (the Info.plist
file is in the Supporting Files folder for the keyboard target).<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>IsASCIICapable</key>
<false/>
<key>PrefersRightToLeft</key>
<false/>
<key>PrimaryLanguage</key>
<string>en-US</string>
<key>RequestsOpenAccess</key>
<false/>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.keyboard-service</string>
<key>NSExtensionPrincipalClass</key>
<string>KeyboardViewController</string>
</dict>
NSExtensionAttributes
dictionary to express the characteristics and needs of your custom keyboard, as follows:IsASCIICapable
—This Boolean value, NO
false
by default, expresses whether a custom keyboard can insert ASCII strings into a document. Set this value to YES
true
if you provide a keyboard type specifically for the UIKeyboardTypeASCIICapable
keyboard type trait.PrefersRightToLeft
—This Boolean value, also NO
false
by default, expresses whether a custom keyboard is for a right-to-left language. Set this value to YES
true
if your keyboard’s primary language is right-to-left.PrimaryLanguage
—This string value, en-US
(English for the US) by default, expresses the primary language for your keyboard using the pattern <language>-<REGION>
. You can find a list of strings corresponding to languages and regions at http://www.opensource.apple.com/source/CF/CF-476.14/CFLocaleIdentifier.c.RequestsOpenAccess
—This Boolean value, NO
false
by default, expresses whether a custom keyboard wants to enlarge its sandbox beyond that needed for a basic keyboard. If you request open access by setting this key’s value to YES
true
, your keyboard gains the following capabilities, each with a concomitant responsibility in terms of user trust:UIPasteboard
classplayInputClick
methodRequestsOpenAccess
key’s value to YES
true
, be sure to read Designing for User Trust, which describes your responsibilities for respecting and protecting user data.