SwiftUI TextField with non-default keyboard does not update the bound value - ios

I am using a TextField to update a numeric value in my app model. The page editing the values is triggered from a NavigationView. Everything works fine when I use a default keyboard, ie:
TextField("Enter a number", value: $userData.chosenNumber, formatter: NumberFormatter())
.keyboardType(.default)
will update the value of userData.chosenNumber when hitting the Return key and this change is reflected on the main navigation screen (userData is an EnvironmentObject).
However, the same code using .keyboardType(.numberPad) instead won't show a Return key, and userData.chosenNumber won't be updated when navigating back to the main view. This happens both in the simulator and live on device (but things work as expected using SwiftUI live preview, or in the simulator when hitting the physical keyboard Return key).
Is there a way to get the new value picked up please without having to use a default keyboard?
--Edit--
Based on pawello2222's suggestion in the comments, I tried the solution in this other SO's question but that didn't fully work in my case.
What the other question suggests: introducing a Binding<String> (via a computed property) that acts as intermediate between the value I want to modify and the string displayed in the UI.
This works to modify the value inside my view.
However the runtime fails to detect that the EnvironmentObject injected by the parent view is modified, so after leaving the view the app UI is not refreshed and the other views do not get updated.
(For some context, my app computes a diver's bottom time based on an air tank site and the diver's air consumption. The detail view is used to edit the diver's air consumption value while another view displays the resulting bottom time. So in my case the diver's consumption gets updated, but the bottom time doesn't receive an update, unlike what happens with a standard keyboard.)

Related

Programmatically Collapse NavigationSplitView to One Column

I have developed a SwiftUI NavigationSplitView app with two columns and it works as expected.
I have chosen dynamic fonts and crafted the visual elements to respond to user settings
for visual accessibility. I know that a number of the users who will employ this app will have
the font size set to the largest available. While that enhances their view of the detail
screen, it pretty much makes the sidebar unreadable. I'm trying to find a way to change
the app's behavior based on the size of the dynamic type. Specifically, I'd like to have
the iPad behavior the same as an iPhone behavior based on a font size that I define.
As it is, the app on an iPhone collapses to a single column. I have not been able to
find a way to programmatically do this on an iPad.
I have tried:
Setting the horizontalSizeClass and as expected, this is a getter only.
.horizontalSizeClass = .compact
I have tried setting the column width of the detail to 0.
.navigationSplitViewColumnWidth(0)
that does not work
I have tried using the old stack function.
.navigationViewStyle(StackNavigationViewStyle())
that does not work.
Clearly, this can be done, since it is automatic when the device is an iPhone. I guess
I could create two views - one stacked and one split and choose based on a State variable
but that seems a bit crude.
Any guidance would be appreciated. Xcode 14 beta 6, iOS 16
This is a great idea and can be achieved by overriding the horizontalSizeClass in the environment depending on the environment value of dynamicTypeSize as follows:
struct NavigationSplitViewTest: View {
#Environment(\.dynamicTypeSize) var dynamicTypeSize
var body: some View {
ViewContainingSplitView()
.environment(\.horizontalSizeClass, dynamicTypeSize > .large ? .compact : .none)
}
}
I tested it by launching the app on iPad simulator and it was in 2 column, then switching to Settings to increase the type size by one notch then back to app and it had switched to single column. However when attempting to switch the size back I noticed the show/hide column button disappeared so I think we have some feedback to submit.
I still have not found an elegant way to do this, but my solution here does work.
Basically, I code for NavigationSplitView and for NavigationStack and change based on the
Environment(.dynamicTypeSize). Like this:
if dynamicTypeSize <= .xxLarge {
NavigationSplitView(columnVisibility: $columnVisibility) {
//all the code
} detail: {
//all the code
}
} else {
NavigationStack {
//all the code
}
}
For others, I was confused by the terms involved here. There is DynamicTypeSize, dynamic
fonts and Larger Accessibility Sizes. Dynamic fonts are those pre-built fonts that
support dynamic type out of the box - .title, .headline, .body etc. Dynamic type size is
the slider in settings that allows the user to scale the dynamic fonts to suit their
needs - all the dynamic fonts scale with the slider setting. Then on top of the slider
in settings for these pre-made fonts, is the option for Larger Accessibility sizes
which gives the user even bigger options.
My scheme above supports all of those intermingled options for both iPad and iPhone.

Appium: How to wait for new element with same name as visible one to appear on screen?

We're using Appium with iOS Simulator and test functions written in Java.
We have an iOS App with screen 1 containing a UICollection view, and tell Appium to click on one of its elements.
This opens screen 2 (and the scrolling animation takes about 500 ms), which also contains an UICollection view. I want to find out the size of the UICollection view of the second screen with Appium.
The problem is that Appium is too fast and executes the findElements() method directly after the click, which causes it to find the UICollection view of the first screen.
clickOnElementOnFirstScreen();
webDriver.findElements( By.className( "UIACollectionCell" ) ).size();
// is supposed to find the UICollection view on the second screen,
// but actually finds the UICollection view on the first screen
Appium provides several waiting functions. However as far as I can see all of them are intended to be used in this fashion:
"wait until element at location X / with name X becomes visible"
If I try to use these waiting functions, they don't wait at all because they immediately find the UICollection view of the first screen, which has the same location and name as the one on the second screen.
The only solution I have found is to use Thread.sleep:
Thread.sleep(1000);
webDriver.findElements( By.className( "UIACollectionCell" ) ).size();
But we don't want to use Thread.sleep in code that will run on the client's server on hundreds of tests.
We might be able to modify the App and enter metadata into the views so that Appium is able to distinguish them, but this situation occurs in several places and the App is being programmed by the client, so we want to avoid this too.
What is a simple and safe way to wait for the new screen to appear, without modifying the code of the iOS App?
I have found only dirty workaround for this issue.
static waitFor(Duration duration) {
try {
def WebDriverWait wait = new WebDriverWait(mobileDriver, duration.standardSeconds)
wait.until(visibilityOfElementLocated(By.xpath("//Fail")))
//Wait until false case is visible to ensure proper timeout
} catch (Exception e) {
}
}
Another workaround/solution that has been posted on the Appium forums is:
First search for some other element that distinguishes the 2. screen from the 1. screen; once that is visible, it's safe to search for the originally desired element.

WatchKit reloadRootControllersWithNames causing error, with pageController or after push/pop

I have a basic watchkit app that loads a page based navigation of 3 interface controllers. This works well, but I'd then like to trigger an action to remove the page-control and essentially revert back to the original InterfaceController that was present when the app first loads.
// load page based control, with 3 views. this works ok
[WKInterfaceController reloadRootControllersWithNames:#[#"pageController1",#"pageController2",#"pageController3"]
contexts:#[#"data1",#"data2",#"data3"]];
// attempt to reload original interface controller, identified by storyboard id
[WKInterfaceController reloadRootControllersWithNames:#[#"myInterfaceController"] contexts:#[#{}]];
The page based navigation remove, the original navigation loads after a short spinner. However it fails to function correctly and original Actions result in this error.
Extension[6766:123665] *********** ERROR
-[SPRemoteInterface _interfaceControllerClientIDForControllerID:] clientIdentifier for interfaceControllerID:(null) not found
Is there a better way to cleanly reload the original InterfaceController?
EDIT, 2/19
It seems there are some other actions that are causing this error too. For instance, if segue to a second InterfaceController and then popController to get back, the error often appears. It is always related to a secondary call to this function.
[WKInterfaceController reloadRootControllersWithNames: contexts:]
EDIT2, 3/18
As previously mentioned, this is reproducible 100% of the time by doing the seguePush, the popController, then attempting to reloadRootControllersWithNames.
If the seguePush/popController is not done beforehand, then the reloadRootControllersWithNames will work fine.
This situation seems to be in addition to the multi->single-multi instance of this bug.
This is actually not a bug because according to Apple:
You cannot combine hierarchical and page-based interface styles. At design time, you must choose the style that best suits your app’s content and design for that style.
So unfortunately, we can't mix Hierarchical and Page-based navigation patterns within the same Watch app.
Just one of many limitations we have to deal with when developing apps for  Watch
This is a bug in WatchKit in Xcode 6.2 Beta 5. Please dupe the following radar on Apple's Bug Reporting System to help raise the priority to get this fixed.
In the meantime, a workaround that I've found can be found on the dev forums. What you can do is add a dummy interface controller to any single interface controller page set so you always have two. This will fix the error until Apple get's the bug fixed (hopefully in Beta 6). Please dupe!
I was able to solve my instance of this problem by not using popController on a pushed view controller. Instead I use a reloadRootControllersWithNames in place of the popController.
How this allows both push and paging, via an example:
Push a view controller
reloadRootControllersWithNames to return to the original controller. (The transition is not quite as animated, but is sufficient)
Create page based view controller.
reloadRootControllersWithNames to return to the original controller
Repeat 1 or 3 as needed.
This eliminates the error at the cost of non-animated popControllers, and allows partial pushing and paging. It would not allow more complex push navigation though.
There may be a better method of navigating to a sub interface controller without a push call, but I'm not aware of it on the watch yet.
None or the answers above worked for me. This problem began when I changed the icon names for the app and the watch app name. I solved it like this:
1) Click on your Watch app Target > Capabilities > make sure app Group
is in ON.
2) Make sure the App Group is selected.
3) Clic on the circled arrow Refresh icon (this will apparently just
refresh this thing if you already had it)
4-Repeat steps 1-3, but for your Watch App EXTENSION target too.
5-Click on the Scheme button (on the right side of the STOP button),
and clic on Edit Schemes.
6-Click Run > Info 7-In executable select your target (Actually it
should already be selecting but opening this window seems to
refresh the option, and wipe the error)
Apparently all these things above are not updated automatically when you change the icon name (Target names) and you have to go to those menus and open them to refresh them manually. Shame on Apple perhaps?

Correct way to display today view widget content

I'm working on a today view extension with some custom view elements which I don't set up in interface builder.
Now I'm wondering where the right point in the lifecycle is to init the widget content.
I read about updating the content in widgetPerformUpdateWithCompletionHandler so I implemented a check for new updates which should about the view.
But my observations showed me that the method is called before viewDidAppearand so there is no view to update.
Also I tought the widget stays in memory for a while so that I can have a certain object in widgetPerformUpdateWithCompletionHandler whose content I can update and use in viewDidAppear but this isn't the case (it loads every time)
So what is the correct way: write content to disk in the widgetPerformUpdateWithCompletionHandler, can I depend on something that is in memory or just ignore and refresh on every load?
In TodayViewController.m -viewDidLoad() method is called each time you open notification. So when ever you check Today widget it will call -viewDidLoad(). So you do your customisation in -viewDidLoad(). Unless you don't want to check previous state.
Straight from a blog which helps a create Today widget with real time data:
Creating today widget
Caching
We can take advantage of NSUserDefaults to save the calculated used space between launches. The lifecycle of a widget is short so if we cache this value, we can set up the user interface with an initial value and then calculate the actual value.
Edit:
From Apple documentation it state that
To help your widget look up to date, the system occasionally captures snapshots of your widget’s view. When the widget becomes visible again, the most recent snapshot is displayed until the system replaces it with a live version of the view.
It is also state that from Notification center, We can get widget details of state and snapshot.
Not sure with this, but it state that below method is used by both widget and containing app to check its content. There is no much details about how to get widget snapshot.
func setHasContent(_ flag: Bool,
forWidgetWithBundleIdentifier bundleID: String!)
NCWidgetController setHasContent
Edit 2: Use of NSUserDefaults in extension:
You have two .entitlements files, one for host app and second for the extension app, add below key in both files
<key>com.apple.security.application-groups</key>
<array>
<string>group.YourExtension</string>
</array>
You see the value for key is group.YourExtension it is shared
common key for both host app and extension app
Save your data using group.YourExtension in NSUserDefaults

Lag When Activating Text Field Inside iOS 8 Custom Keyboard

I want to add a search field inside my custom keyboard that allows the user to search for content which can then be input into the main text document for which the keyboard is being displayed.
However, calling -becomeFirstResponder on the UITextField object causes a 3-4 second lag during which no input is accepted. The same thing happens if I call -resignFirstResponder. I can set the text property of the text field just fine, but that means that there is no caret, and the text is truncated to fit.
On calling (become|resign)FirstResponder, I get the following messages in the log:
2014-10-28 13:39:50.920 Giffy KB[2073:514844] Received 0 images
2014-10-28 13:39:57.942 Giffy KB[2073:514745] <_UIRemoteInputViewController: 0x14762a030> timed out waiting for fence barrier from com.theappical.giffage.giffagekb
2014-10-28 13:39:58.446 Giffy KB[2073:514745] View service did not balance fencing 'begin' messages with 'end' messages within a second; timing out.```
Other third party keyboards seem not to use textfields (they use custom views instead). Does that mean this is a bug at Apple's end, or is there something I can do to resolve it?
Filed a bug report with Apple for the same, ended up faking a text field using a UILabel. As of now (iOS 8.1 latest), this issue has yet to be resolved.

Resources