How do I implement turn timeout with gkturnbasedmatch - ios

I want to implement a turn timeout feature in my game center app. It should give each user a 30-60 sec time to complete their turn and if not they should be treated as they quit. I tried using the timeout parameter of endTurnWithNextParticipants but it doesn't seem effective with small values. It never signals a turn timeout, afais the default value for it is 1 week (GKTurnTimeoutDefault). So I guess apple had different ideas than mine while using the name turnbasedmatch.
My best bet is to set a timer when user receives the turn for 30-60 secs, and if user does not make a turn in that period, fire one of the participant quit methods. But it is not a reliable solution because users may run out of batteries, turn off the phone also nstimer seems to pause ticking during uitouchevents (uiscrollview scrolling etc, maybe it can be handled by running nstimer in another thread i don't know). Any tips on implementing turn time out reliably?

Related

How does Chromecast determine inactivity?

I've made a Chromecast app that displays multiple quotes and the whole idea is to basically connect it to a spare tv/monitor you have lying around and use it to have inspirational quotes change on the screen.
But the problem is, Chromecast automatically times out and goes back to the home screen after every few minutes of inactivity. I'd like to find out how it detects inactivity so I can prevent this behavior and allow my app to keep running on the Chromecast indefinitely, as a sort of replacement for the original photo screensaver.
I figured out how to keep my app running. There is a property of CastReceiverOptions called disableIdleTimeout that can be set.
const options = new cast.framework.CastReceiverOptions();
options.disableIdleTimeout = true;
const instance = cast.framework.CastReceiverContext.getInstance();
instance.start(options);
With this set to true, the receiver no longer times out after 5 minutes.
You may check the setInactivityTimeout method then use the maxInactivity parameter which is interval in seconds before closing an unresponsive connection.
The setInactivityTimeout(maxInactivity) sets the receiver inactivity timeout. It is recommended to set the maximum inactivity value when calling Start and not changing it. This API is just provided for development/debugging purposes.
You may also refer with this SO answer which stated that:
Timeout value cannot be adjusted by application. It would help us to understand what is causing the timeout in your case. Timeout happens when either the sender does not reply to the receiver's ping requests in a timely manner, or when the sender has not received a ping from the receiver for a certain length of time. It might be the case that the media you are trying to load is tying up the CPU on the receiver so that it can't send its ping request to the sender.
Hope this helps!

Running background tasks ending with a local notification in swift?

This is in response of an effort in order to implement an app in a way, so that I could run some code at specific interval of time i-e Morning 9:00 am daily.
Lets take an example of an Alarm.
If user is allowed to set a time interval, during which he is going to remain busy, so now after setting two - three intervals for his daily schedule.
After the app is suspended or in foreground, user needs a reminder before entering to and after getting out of the interval.
I need guidance for this problem in a way :
Local notifications for this situation in swift
Changing the mode of device from ringer to silent during that interval
OR
Accessing programatically 'Do not Disturb Me' feature of IOS
Your kind conveyance regarding this issue will be really productive for me.
Thanks.

Add life every N hours

I want to implement mechanism that will be add one life to UserDefaults.standard.value(forKey: "lifes") every 4 hours even if app was closed and notify user about it.
About notifications i read articles, it will not be hard. But how increase life number value if i didn't be in game for 12 hours. I should get +3 lifes when back.
You get an event when the game is paused (UIApplicationWillResignActive) so you can write down in user defaults what time it was. You get an event when the game resumes (UIApplicationDidBecomeActive) so you can check to see what time it is and how many hours have elapsed since you became inactive.
This is not possible if the app is "closed".
The only way to achieve this behaviour is to use Push Notifications (pushed by your server, not the app).
See this and this Apple documentation.
My opinion is, that the user will delete your app, as she will get disturbed by too frequent notifications.

Can a background app (with a location UIBackgroundMode) use a timer to poll a server every few hours?

I have a background app with a UIBackgroundMode of location.
I would like my app to additionally contact a server every few or several hours to see if there is some new data for it (because using apple notification push would notify the user and that is not desirable).
Polling is something I would never use on any other OS, but with iOS they don't leave you much choice if there is certain functionality you would like to try to achieve.
If the polling interval is quite lengthy such as a few or several hours between polls, and the polling activity itself only lasts several seconds then the usual knee-jerk reaction about it draining battery life is greatly diminished.
Would a repeating NSTimer fire when an app is in background mode? If not is there another type of timer or mechanism available?
If it's just to check for new content, and not really time sensitive, you COULD use the significantChanges background location method...but if the user stayed fairly immobile it'd rarely/never fire. I would probably also add the update check in applicationWillEnterForeground to be more sure
No, that's not allowed. You should have a look at Push Notifications and find a server side solution.

Track time spent in application

What should I use to track how long is user using my application on iOS devices?
I think I can do it like this:
Log time on App startup (application:didFinishLaunchingWithOptions:)
Log time on App ending (applicationWillTerminate:)
List item
Send report (pairs of start/exit time) to server and count time spent in app
Do you have any better ideas?
Important is to be network and resources effective
(not to send big amount of data or consume iOS resources on complex computing operations.)
Notice that applicationWillTerminate: is not necessarily called when the home button is pressed (this changed from iOS 3.X to iOS 4.0).
Take a look at applicationDidEnterBackground: and applicationWillEnterForeground:
More here.
I believe that calculating the time on the device itself would be best, considering you want this to be network and resource effective. The calculation of endTime - starTime will be very very fast (depending on your time implementation), and then you would only need to send one piece of data (the time it took) over a network instead of two (the start and end times).
Hope this helps!
N.S.
Seems like a reasonable way to do it. It's efficient enough, and depending on how you see the data, it shouldn't be more than a kilobyte or two, if that. Be wary of privacy issues though—users are really against being logged.
Your method is sound, it will have next to no overhead and get the job done nicely.
Log time on App startup (didFinishLaunchingWithOptions)
send report often with interval of 5min.
send report in applicationWillEnterbackground.(pause timer(invalidate) if u used background thread for it).because if the user enter in other app then they can close ur app by just using home button and with minus button in app manager.so u have to send report here also
resume timer with previous time in applicationWillEnterForeground.
Log time on App ending (applicationWillTerminate)
Send report (pairs of start/exit time) to server and count time spent in app

Resources