Nitin Gohel
Nitin Gohel

Reputation: 49710

How to use auto suggestion view for custom key-board extension ios8?

How can I implement Apple's predictive input panel in my own iOS8 custom keyboard extension?

Apple's Custom Keyboards API Doc Custom Keyboard states:

RequestsOpenAccess set BOOL yes in info.plist 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.

But i can't find how to use the UILexicon in my custom keyboard. I set the RequestsOpenAccess setting to YES:

enter image description here

But still can't get access to a custom dictionary for word suggestions like Apple's iOS8 default keyboard does:

enter image description here

My custom keyboard look like:

enter image description here

EDIT:

i Found requestSupplementaryLexiconWithCompletion that used for UILexicon class like this i try to implement this using 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];
        }
    }];
}

Upvotes: 7

Views: 3997

Answers (2)

Vishnu Kumar. S
Vishnu Kumar. S

Reputation: 1837

May be 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'];
}

Upvotes: 0

Nitin Gohel
Nitin Gohel

Reputation: 49710

Finlay i done it..! i put suggestion using sqlite static database in and get first three suggested work using like query like 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 it my keyboard output is:

enter image description here

Upvotes: 8

Related Questions