How to reload a view in the apple watch? - ios

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.

Related

CMClock Swift Example

I have two iOS devices physically separate from each other. I need them to perform a task at exactly the top of every minute and bottom of every minute (so every 30 seconds) and stay synchronized.
My initial approach was to calculate the time until the top of the next minute and set a timer until that time and then start my 30 second timer.
The problem was that the 30 second timer would drift and eventually be out of sync.
I discovered an Apple API called CMClock. I could not find an example but was wondering if anyone has used this API and if so could provide an example of how to keep two devices synchronized like I described?

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 ?

iOS: Determine if another app becomes active

My app needs the device location with an accuracy of about 10 meters. If the app is launched, it usually takes e.g. 10 sec to get the required accuracy. This delay is OK. However when the app is „in use“ (see below), the delay should be less, e.g. 1 sec.
The problem is the following:
When the user switches off the display, the app transits from the active state to the background state (and the delegate methods applicationWillResignActive and applicationDidEnterBackground are called).
Normally, location updates are not done in background. So, next time the app transits from background to active state (delegate methods applicationWillEnterForeground and applicationDidBecomeActive are called), the location manager needs again e.g. 10 sec to reach the required accuracy. This delay in unfortunately not OK.
To avoid it, the app could do location updates in the background. This works fine.
The disadvantage is that these background location updates continue, even if the app is no longer used, because the user pressed the home button and switched to another app. This is disturbing at least for 2 reasons: The GPS hardware is unnecessarily active and uses power, and the user is notified that my app is using the device location although this is no longer required.
My question thus is:
Is it possible to determine if another app becomes active?
If so, background location updates could be switched off.
Is it possible to determine if another app becomes active?
No, your app only knows if it's in background or in foreground handling delegate events. Know if user opened another app or is in the home screen is not possible.
These background location updates continue, even if the app is no longer used, because the user pressed the home button and switched to another app.
I think you're right. You could start a timer when application goes in background and update localization only for an estabilished period (one minute?). Then, at the end of timer count save last localization coordinates. Timer is used to avoid unnecessary updates, then if user doesn't open again your app after a reasonable time, maybe it's because he's using other apps or he locked the device.
When the app comes to the foreground again, if timer is still active you're good. Otherwise show last localization saved and show a small "banner" to advise user that localization could not be accurated for the first 10 seconds (until when required accuracy has been reached)

How to keep the NSTimer always work evenly

Now I am creating a metronome program. I use a NSTimer to bring the metronome into play. Of course The Timer works repeatedly. But I find out in two situation that the timer works not accruately.
When just start the NSTimer, the first two beat sometimes goes too closely. After then, the beat goes evenly.
When the app goes backgound I make the Timer work continuely by:
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
but sometimes the unevenly beat also happens at the time when entering or coming back from the background status.
So I want to know how to keep the Timer always work evenly, no matter which situation it is in. Thks!
When setting up a timer firing at a certain time, the system implicitly sets an allowed "leeway" which is a short delay within the timer may actually fire.
This is due to "Timer Coalescing" - a system feature - which groups events happening roughly at the same time together and fire them at the exact same time. The reason is to reduce CPU cycles and extend the idle time of the CPU to save power.
If the timer interval is small (milli seconds to seconds) this "leeway" is in the range of 10% of the timer interval.
You can explicitly retrieve and set the current maximum allowed leeway value with the methods
-tolerance
-setTolerance:
See also:
NSTimer
Timing Accuracy

Timer and Dispatcher Timer in Emulator vs. Device

I need to use a timer in my WP7 application and display it's value in the UI.
I managed to get it working two ways: one using the DispatcherTimer class and the other using the Timer class.
The problem is that the DispatcherTimer is slower than "real-time" and the Timer class actually goes faster than "real-time". So both go wrong.
Is this because I'm running it in the emulator? Don't have a device to test it on. Anyone had this issue and tested on both?
To show time accurate to one second on screen, I use a DispatcherTimer that updates every 500 milliseconds and then subtract the current time from the start time to get the time elapsed and display that on-screen.
Does that help?

Resources