How to Change AccessibilityLabel on runtime in iOS Swift? - ios

I am showing a 30 min timer in my iOS application, I want to change the accessibilityLabel of the timer on runtime so that whenever the user taps it, the talkback will read the latest time. Right now the Label gets set to 30 min and whenever the user taps on the timer the talkback reads 30 min.

Try :
UIAccessibility.post(.notification: .screenChanged, argument: timer.text)
You want to use a .screenChanged notification because whenever the timer changes, the screen is changing as well. Your argument is the text that is changing.

Related

Does NSTimer works in conjunction with device's clock?

If we manually change the clock time of device, will the NSTimer output change? I want to show a count down, but I am not sure if user will change the device in-between then will it change the NSTimer outcome too ?

how to give to activity alert in iOS swift?

Suppose App is in foreground and user does not interact with app for 5 min, App should give alert.
Suppose App is in background and it remain in background for more than 5 min App should give alert as soon as app comes in foreground.
Is there any standard way to do that?
For the background part, basically you want to have a Counter. This Counter should do
Observe UIApplicationWillResignActiveNotification notification. And record the time when the selector is called. Let's say it's lastActiveTime.
Observe UIApplicationDidBecomeActiveNotification notification. And inside the selector, compare the current time with lastActiveTime. If it's more than 5 minutes, you'll pop up the alert.
For foreground, you could use some assumptions such as if the top most view controller is the same, assume the user hasn't interacted with the app. You can have a timer that keep checking the top most view controller.

How to reload a view in the apple watch?

Hi every one i have to update instantly from my parent app (iPhone App)
and Show the value in the watch App. I have to reload the value in the
watch screen when ever the apps values get updated. I have to update from the app and show it in its extension app.
if change the location in the iPhone app. Automatically the values should be changed in watch App. I'm using App Group and NSUserDefault to share values between these apps.Now i can access values from the watch App but its not get updated automatically. Thanks in advance
You can set a timer and change the contents of labels.
In WatchKit (WKInterfaceLabel), setting label's contents is very easy. Just use the following code in order to set a new text for labels:
Swift:
label1.setText(temperature)
Objective-C:
[label1 setText:temperature];
Also you can do the same with pictures, charts etc. Just insert the code said before in a function, and call that function each time the timer finishes its interval time.
It's better to set short intervals for timer. Less interval means refreshing faster but more internet usage and battery drain, and longer intervals means refreshing not fast as previous way, but less internet usage and more battery saving.
You can change the interval in run-time by battery percentage. If it is - for example, less than 20% - you can set longer intervals, and when it is more than 20%, set shorter ones. You can only find Apple Watch battery percentage in watchOS 2, but in both versions you can get iPhone battery percentage by the following code:
Swift:
var x = UIDevice.currentDevice().batteryLevel
Objective-C:
int x = [UIDevice currentDevice].batteryLevel;
NOTE: Short intervals are from 10ms to 200ms, and long ones are from 200ms to 1s. (ms = Milliseconds = 0.001 seconds)
NOTE: You can call the initialization method of the view in timer intervals.
If you want to update view manually, just make a button, and set labels and charts' data (by the code given before) or call the view init method.

Where to update the UI of a notification widget just once

In my Notification Center Today Extension/Widget I need to update a portion of the UI every time Notification Center is activated. It never needs to update while Notification Center is in use, nor while it's in the background. In what method should I place that code?
viewDidLoad and viewWillAppear are both called every time it will be displayed, for example if you scroll up and down they will be called again, so that's too often.
widgetPerformUpdateWithCompletionHandler is not called at all before it's displayed for the first time it seems (at least with iOS 8.2 beta), and this method is automatically called whenever iOS feels like it to update the UI even when it's in the background which is not appropriate either.
loadView is only called a single time, never to be called again unless the widget is removed from memory. So if you open Notification Center and view the widget then dismiss Notification Center and reopen it later, it may not call that method again depending on whether or not it's been cleared from memory.
I'd just use viewDidLoad and not worry about the possibility of multiple calls. Unless the method takes a long time to run, there's no reason not to do it that way. [And if it does take a long time to run, your today extension is going to suck, so fix that.]
If for some reason you only want it to happen once, add a BOOL ivar to the class. Set it to YES in initWithCoder: Then in viewDidLoad, check that value. If it's YES, do your update and set the value to NO. If it's already NO, skip your update.

Is there an easy way to detect *any* (and all) user activity?

If the user interacts with my app in any way at all I want to restart a force logoff timer.
Any menu item, any button clicked (not sure about just click on the form).
Is there an easy way to detect that the user is still "active"?
This component is exactly what you are looking for. It is called ETimer and can be found here
Basically just put the timer on a form and set its interval for like 1000 ms, then in the timer event check the Snooze property of the timer, if the snooze property is greater that 60000 (1 min) then you know that the app has had no user interaction for 1 min.
It pretty easy to use but let me know if you get stuck.

Resources