如何在iOS8自定义键盘扩展中使用自动建议视图?

7
我如何在自己的iOS8自定义键盘扩展中实现苹果的预测输入面板?
苹果的自定义键盘API文档Custom Keyboard指出:
“在info.plist中将RequestsOpenAccess设置为BOOL yes,可以通过UILexicon类访问基本的自动更正词典。利用这个类和你自己设计的词典,为用户在输入文本时提供建议和自动更正。”
但我找不到如何在我的自定义键盘中使用UILexicon。我将RequestsOpenAccess设置为YES:

enter image description here

但仍然无法像苹果iOS8默认键盘一样获得自定义词典以进行单词建议:

enter image description here

我的自定义键盘长这样:

enter image description here

编辑:

我发现了requestSupplementaryLexiconWithCompletion,它用于UILexicon类,像这样,我尝试使用以下代码实现:

 - (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];
        }
    }];
}
2个回答

8

Finlay,我做到了..!我使用SQLite静态数据库提出了建议,并使用类似以下代码的查询获取了前三个建议:

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];
    }

我已经搞定了,我的键盘输出如下:

enter image description here


你是如何填充SUGGESTION表的?你从哪里获取所有的建议? - iOS Dev
我在我的回答中提到我使用了静态SQLite数据库,并使用LIKE查询从SQLite数据库获取相关字符建议。@StanleyKubrick - Nitin Gohel
这种方法被苹果认可了吗?否则我的键盘会被拒绝吗? - Massimo Piazza
我也实现了这种方法,唯一的限制是无法识别打字错误,因此,例如我输入"helo"时将不会得到任何建议。 - Massimo Piazza
你从哪里获得数据库的? - TomSawyer
显示剩余3条评论

0

也许这个答案能帮助到某些人。

+ (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'];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接