Watchkit: Single Tap Text Dictation through WKInterfaceController

600 Views Asked by At

According to Apple's doc of WKInterfaceController you can let user to dictate text preseting a new interface controller in this very simple way:

self.presentTextInputControllerWithSuggestions(["suggestion 1", "suggestion 2"] allowedInputMode: .Plain, completion: { (answers) -> Void in
if reply && reply.count > 0 {
    if let answer = answers[0] as? String {
        println("\answer")
    }
}

})

as explained here.

I have seen that Amazon App for Apple Watch let you search for products by tapping the search icon directly

enter image description here

So you get into the Dictation in one step

enter image description here

Through the WKInterfaceController method, we will get something different

enter image description here

Which Apple's API Amazon app is using to enable dictation in this way?

(UPDATE) I have just find out that it's super easy as explained here

So the final solution I came out was this

    - (void) table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
if (rowIndex==0) { // handle manual selection
    __weak MainInterfaceController *weakSelf = self;
    [self presentTextInputControllerWithSuggestions:nil allowedInputMode:WKTextInputModePlain completion:^(NSArray *results) {
       if(results && [results count]>0) {
           NSString *inputText=nil;
           for(NSString *input in results) {
               NSLog(@"Input %@", input);
               inputText=input;
               break;
           }
           if(inputText!=nil && [inputText length]>0) {
               [weakSelf pushControllerWithName:@"Search" context:
                [NSDictionary dictionaryWithObjectsAndKeys:
                 inputText, @"query", nil]
                ];
           }
       } else {
           NSLog(@"No input provided");
       }
   }];
}}
2

There are 2 best solutions below

0
On

Set the mode to .Plain and don't provide any suggestions.

0
On

If you remove the suggested strings array, it will go straight into dictation mode.