How to use auto query for custom ios8 keyboard extension?

How can I implement Apple predictive pane in my own iOS8 keyboard extension?

Apple Custom Keyboard API Custom Keyboard API:

RequestsOpenAccess set BOOL yes, in info.plist there is access to the basic autocorrection lexicon through the UILexicon class. Use this class along with the lexicon of your own design to provide suggestions and autocorrections when users enter text.

But I can not find how to use UILexicon in my user keyboard. I set RequestsOpenAccess to YES :

enter image description here

But still can't access the user dictionary for word sentences, such as the default Apple iOS8 keyboard:

enter image description here

My custom keyboard looks like this:

enter image description here

EDIT:

i Found the requestSupplementaryLexiconWithCompletion that was used for the UILexicon class , like this, I am trying to implement this using the following code:

  - (void)viewDidLoad { [super viewDidLoad]; [self requestSupplementaryLexiconWithCompletion:^(UILexicon *appleLex) { appleLexicon = appleLex; NSUInteger lexEntryCount = appleLexicon.entries.count; for(UILexiconEntry *entry in appleLexicon.entries) { NSString *userInput = [entry userInput]; NSString *documentText = [entry documentText]; lable.text=userInput; [lable setNeedsDisplay]; } }]; } 
+7
ios iphone ios8 keyboard ios-keyboard-extension
source share
2 answers

Finlay, I made it ..! I suggest using a static sqlite database and getting the first three suggested jobs using the same query as the following code:

 NSString *precedingContext = self.textDocumentProxy.documentContextBeforeInput; //here i get enter word string. __block NSString *lastWord = nil; [precedingContext enumerateSubstringsInRange:NSMakeRange(0, [precedingContext length]) options:NSStringEnumerationByWords | NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange subrange, NSRange enclosingRange, BOOL *stop) { lastWord = substring; *stop = YES; }]; NSLog(@"==%@",lastWord); // here i get last word from full of enterd string NSString *str_query = [NSString stringWithFormat:@"select * from suggestion where value LIKE '%@%%' limit 3",lastWord]; NSMutableArray *suggestion = [[DataManager initDB] RETRIVE_Playlist:str_query]; NSLog(@"arry %@",suggestion); i get value in to array using like query if(suggestion.count>0) { if(suggestion.count==1) { [self.ObjKeyLayout.FirstButton setTitle:[suggestion objectAtIndex:0] forState:UIControlStateNormal]; } else if(suggestion.count==2) { [self.ObjKeyLayout.FirstButton setTitle:[suggestion objectAtIndex:0] forState:UIControlStateNormal]; [self.ObjKeyLayout.secondButton setTitle:[suggestion objectAtIndex:1] forState:UIControlStateNormal]; } else { [self.ObjKeyLayout.FirstButton setTitle:[suggestion objectAtIndex:0] forState:UIControlStateNormal]; [self.ObjKeyLayout.secondButton setTitle:[suggestion objectAtIndex:1] forState:UIControlStateNormal]; [self.ObjKeyLayout.thirdButton setTitle:[suggestion objectAtIndex:2] forState:UIControlStateNormal]; } } else { [self.ObjKeyLayout.FirstButton setTitle:@"" forState:UIControlStateNormal]; [self.ObjKeyLayout.secondButton setTitle:@"" forState:UIControlStateNormal]; [self.ObjKeyLayout.thirdButton setTitle:@"" forState:UIControlStateNormal]; } 

and I got its output on the keyboard:

enter image description here

+8
source share

Maybe this answer helps someone.

 + (void)getSuggestionsFor:(NSString *)word WithCompletion:(void(^)(NSArray *))completion { NSString *prefix = [word substringToIndex:word.length - 1]; // Won't get suggestions for correct words, so we are scrambling the words NSString *scrambledWord = [NSString stringWithFormat:@"%@%@",word, [self getRandomCharAsNSString]]; UITextChecker *checker = [[UITextChecker alloc] init]; NSRange checkRange = NSMakeRange(0, scrambledWord.length); NSRange misspelledRange = [checker rangeOfMisspelledWordInString:scrambledWord range:checkRange startingAt:checkRange.location wrap:YES language:@"en_US"]; NSArray *arrGuessed = [checker guessesForWordRange:misspelledRange inString:scrambledWord language:@"en_US"]; // NSLog(@"Arr ===== %@",arrGuessed); // Filter the result based on the word NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@",word]; NSArray *arrayfiltered = [arrGuessed filteredArrayUsingPredicate:predicate]; if(arrayfiltered.count == 0) { // Filter the result based on the prefix NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@",prefix]; arrayfiltered = [arrGuessed filteredArrayUsingPredicate:newPredicate]; } completion(arrayfiltered); } + (NSString *)getRandomCharAsNSString { return [NSString stringWithFormat:@"%c", arc4random_uniform(26) + 'a']; } 
0
source share

All Articles