Day 29 and 30 – WordScramble App

,

The WordScramble App was a partial success on watchOS. There are some big ‘gotchas’ that mean some further knowledge of workarounds would be needed to make it truly work well on the most personal device.

Modifiers

Strangely in watchOS, I couldn’t get the textField modifier .autocapitalization(.none) to work. The editor would throw a Cannot infer contextual base error. However, I was able by using Xcode auto-completion I could use .textInputAutocapitalization(.never).

I first tried the new modifier with .none but the keyboard on Apple Series 7 and above would still capitalise the first letter input, whereas, .never work as expected.

UITextChecker         

However, there are two BIG problems with coding this App for the Apple Watch. The first is that UITextChecker() does not exist in watchOS. The second is that .onAppear works a little differently on the watch.

The lack of the UITextChecker API means that your would need to find a watchOS compatible library to check a work is spelt correctly. I’ve tried search Apple’s documentation on the Apple site and can not see any other alternative for watchOS. Indeed, at the moment I don’t see an option for SwiftUI in general. The UI in the UITextChecker name showing that this method is part of the older UIKit.

onAppear

Secondly, relying on .onAppear will not work for the watchOS version of the Word Scramble App. This is due to the smaller screen size, which means that views disappear more often. For instance, when the text input View is shown. The by-product of this odd behaviour is that the startGame method is called again as the keyboard dismisses and your word is very likely not compatible anymore.

I’m not sure what is happening here as I can not see anything happening as the view disappears using .onDisappear(perform: { print("View hidden") }) but the .onAppear() is absolutely firing off again when the keyboard dismisses.

In order to cope for this unexpected behaviour I added an extra boolean to the App

@State private var hasGameStarted = false

Then I added this line of code to the startGame method to ensure a new word is not generated.

guard hasGameStarted == false else { return }

Overall there are some big issues moving the code base to watchOS this time, one last thing I would mention is that because the keyboard/text input screen on the watch fully covers the screen, you can not see your root word when entering your own. Therefore I would suggest a little workaround of changing the TextField("Enter your word", text: $newWord) to TextField(root word, text: $newWord) so at least you still have sight of it up until you enter your try.

*Since watchOS 9 there has been a keyboard available on the Apple Watch. I wasn’t sure if this was only on the bigger watches, but it seems to show up on the 41mm watch in the simulator. is the keyboard only available on Series 7,8 and Ultra watches?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.