2014-09-26 16 views
7

Come posso implementare il pannello di input predittivo di Apple nella mia estensione personalizzata per tastiera iOS8?Come utilizzare la visualizzazione automatica dei suggerimenti per l'estensione della tastiera personalizzata ios8?

di Apple Tastiere personalizzate API Doc Custom Keyboard stati:

RequestsOpenAccess set BOOL sì in info.plist ha accesso a una base autocorrection lessico attraverso la classe UILexicon. Utilizzare di questa classe, insieme a un lessico di propria progettazione, per fornire suggestions e autocorrections mentre gli utenti inseriscono il testo.

Ma non riesco a trovare come utilizzare il UILexicon nella mia tastiera personalizzata. Ho impostato il RequestsOpenAccess impostazione per YES:

enter image description here

Ma ancora non è possibile ottenere l'accesso a un dizionario personalizzato per i suggerimenti di parole come la tastiera di default iOS 8 di Apple fa:

enter image description here

mia tastiera personalizzata aspetto:

enter image description here

EDIT:

ho scoperto che requestSupplementaryLexiconWithCompletion utilizzato per UILexicon class come questo cerco di implementare questa seguente codice utilizzando:

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

risposta

8

Finlay ho fatto ..! ho messo suggerimento usando database statico SQLite in e ottenere primi tre lavori ha suggerito di utilizzare come interrogazione come codice seguente:

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

e ho ottenuto la mia uscita tastiera è:

enter image description here

+0

Come è stato riempito il tavolo SUGGESTION? Dove hai ricevuto tutti i suggerimenti? –

+0

ho accennato nella mia risposta ho usato il database sqlite statico e usando la query LIKE ho ottenuto il lavoro di suggerimento relativo ai personaggi dal database sqlite. @StanleyKubrick –

+0

Hey Nitin dove sarà il nostro file di database? nell'app principale o nell'estensione? – jamil

0

Può essere questa risposta aiuta qualcuno

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