Issues using UITextInput/UIKeyInput implementation with captureTextFromCamera

Description

I'm (trying to) use captureTextFromCamera(sender:) however I'm having issues getting both insertText(text:) and setMarkedText(markedText:, selectedRange:)/unmarkText() to work at the same time. I have attached the relevant portion of the UIView subclass here:

and can provide a minimal reproduction project if requested.

Details

I subclassed UIView and implemented the relevant methods/properties for UITextInput, however I found that while the markedText methods were being called insertText wasn't.

I then changed my subclass to declare it's conformance to UIKeyInput instead of UITextInput (didn't change anything else) and now insertText gets called, but setMarkedText/unmarkText no longer get called (as expected).

I don't think insertText not being called is expected based on what was said in the session and what I can find online. However I have never worked with these protocols before so I may be misunderstanding how they work.

SwiftUI

I'm doing this from a SwiftUI app using UIViewRepresentable, and I'm not actually showing the UIView anywhere, just including it in the hierarchy to be able to call captureTextFromCamera and get the relevant protocol methods called to capture the text.

I have filed feedback with a sample project here: FB9155790.

Thanks for the question!

class CameraUIView: UIView, UIKeyInput {

You should probably use UITextInput and not UIKeyInput if you'd like to use the marked text methods. Although you'll likely have to implement the rest of the UITextInput methods.

And if you implement UITextInput you might actually not see insertText called. Instead you'll see setMarkedText:selectedRange: for the preview and unmarkText when the user hits the 'insert text' button. Sorry for the confusion. Hopefully that helps.

When I implement UITextInput I do get the exact behavior you predicted where insertText is not called but setMarkedText and unmarkText are called. However that makes it so that after text is recognized and passed to setMarkedText I cannot distinguish between the user pressing insert and the user pressing the "x" to dismiss the camera. Both result in 2 calls to unmarkText. In one case I'm supposed to save the text, in the other I'm supposed to get rid of it.

Our use case is for the user to scan 1 word from an object that has a lot of words around the word we want to scan. We need to the user to be able to cancel without dumping a long string of junk into the textfield, which currently seems to be the only option.

Is there any way to tell if the user selected Insert or cancelled out of the view?

If the user cancels, you should receive setMarkedText: with the empty string.

I just gave it a try and here is what I found:

If I get preview "marked text", then cancel I get these calls in this order:

  1. setMarkedText: with "".
  2. setMarkedText: with "some string".
  3. setMarkedText: with "".
  4. unmarkText.

If I hit insert I get these calls in this order:

  1. setMarkedText: with "".
  2. setMarkedText: with "some string".
  3. unmarkText.
  4. setMarkedText: with "".

So if I understand correctly that means that if I receive unmarkText while there is marked text it should be inserted. I gave that a try in my code and it works! Thanks!

Upon re-reading the documentation That now makes sense. I think that if I was actually fully implementing UITextInput I would have figured it out. However since I was only using the methods mentioned in the talk for camera I didn't have the extra context, even after reading the UIKeyInput and UITextInput documentation.

I will update my Feedback with this information, and I think it basically turns into some combination of:

  1. When using the captureTextFromCamera the behavior of UIKeyInput and UITextInput don't make sense, since changing the declared protocol conformance changes the behavior.
  2. The session implies that you can just implement UITextInput to get the marked text (e.g. preview text) behavior without changing the logic from your UIKeyInput conformance for dealing with the "inserted" text.
Issues using UITextInput/UIKeyInput implementation with captureTextFromCamera
 
 
Q