I'm working on VPN for iOS. And i want to add ability to pause vpn to 5, 15 min and a hour. It means when user for example clicks on pause 15 min vpn disconnected and start connection in 15 min. But i don't understand how to schedule connection task.
I tried to do it with Timer but timer stop working when app in background.
I thought about BGTaskScheduler but how i understand in this case OS decides when start task and i will not be able to guarantee that i will start vpn connection in time.
Also i thought about silent PUSH notifications but i not sure that it is a good solution.
Can anyone suggest how can i solve this task more accurate?
Related
My app listens for 3 geofences while the app is closed. When entering a geofence, the identifier is sent to the back-end. Everything works wonderful but I would like to also handle the case when there is no network connection when entering the geofence. Like waiting with sending the request until there is a connection. This has to work when the the app is closed.
It only has to work on devices with iOS 11 or higher. Waitsforconnectivity does not work, I think because the time window from the geofence is to small. When i start the app that specific request is successfully sended so it does work but not if the app is closed. I have to manually start the app to let the pending request send when there is a connection. I also tried dataTask and uploadTask but these give the same behaviour as with waitsForConnectivity. I also think that is not possible to check if there is a network connection with scheduling a task with a timer in the future? Because the app is closed.
Is this even possible? I think the only way to do this is with a remote push notification but running code as a response to a push notification is only allowed with Voip.
I find it hard to believe that this isn't possible since this so easy with a BroadcastReceiver on Android.
Try BackgroundFetch
You can wake your app with certain periodic interval with this approach, without user interaction. Once your app wakes up, check if you have some pending data to be sent to the server. If yes, send it. If the internet connection is not there, no problem, program your app to wake up at let us say every 5 hours.
// Fetch data once an hour.
UIApplication.shared.setMinimumBackgroundFetchInterval(3600)
A simple call like above will wake your app up after 1 hour. Follow the steps in the link and you'll get the idea.
So I need this app to run timer for more than 3 minutes, and play a sound like very 45 seconds, most of the solutions here are just for less than 3 minutes on iOS. Is there a way to make it run all the time, unless the app stops?
After the research, I guess the solution is implement beginBackgroundTaskWithExpirationHandler
and stops it by giving a handler and set location update on plist?
As for audio, besides setting the plist, anything else need to do to play audio?
Swift how to use NSTimer background?
iphone - NSTimers in background
Run app for more than 10 minutes in background
No, in order to save all of us from apps that kill our batteries, Apple does not allow apps to continue to run in the background for more than a few minutes. There are only very special limitations (music playing apps, VOIP, navigation apps, etc.) which permit ongoing operation. In terms of details, this is described in About the Background Execution Sequence.
If you want to notify user of something at some future time, you can use local notifications. When you do this, though, you don't control whether your app restarts or not. Only if the user taps on the notification (assuming they even granted your app notification privileges at all), will the app be reopened. For more information, refer the the local notification discussions in UserNotification framework documentation. But note, this is not intended for alerts every 45 seconds, but rather for a significant notification scheduled for some future time.
For discussion of how one might marry local notifications with timers, see swift NSTimer in Background and this follow up question swift calculate time for timers running in background.
In app when using NSTimer then this timer is consume battery of iOS device..?
We are schedule this NSTimer for every 30 SEC in app whether app in foreground or background.
In this timer we just check internet availability by sending request to host.
You can use this nice and simple block based, drop dead Reachability class to get notified whenever there is any change in network availability.
https://github.com/tonymillion/Reachability
Do not use a Timer to get notified for network connection changes.
On connect i added this bit of code at the end of getting the user connected:
UIBackgroundTaskIdentifier myLongTask;
myLongTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
// If you're worried about exceeding 10 minutes, handle it here
}];
CFRunLoopRun();
[[UIApplication sharedApplication] endBackgroundTask:myLongTask];
I also set in build settings, application needs wifi. This is a basic client that uses a socket.
Before if i locked the iPad the internet died. Now i was expecting it to stay alive for 10 minutes after the iPad locks but for some reason it just stays on indefinitely so far. I've measured the internet being on over 30 minutes after i lock the phone and come back and unlock and my connection is still streaming chat.
I'm concerned this may not be legal app behavior for acceptance in app store though i did not set any special settings i was reading about that this is a VOIP app. I just used the beginBackgroundTaskWithExpirationHandler, and application requires internet. I'm also concerned about long term battery impact if indeed the application never really sleeps, though the server i'm connecting to would disconnect you if idle for 60 minutes so it would not stay connected indefinitely but still its a concern if this loop always runs even after a disconnect it could use battery. For example what if the user leaves the app and doesnt come back for over a week and the iPad is just sitting locked for a week would it use accelerated battery? My testing hasn't got that far yet as this is a new situation this morning that i have were internet wont turn off were before it shut the connection any time the screen locked.
Any suggestions? Anyone know why the app wont sleep after 10 minutes of a screen lock?
Mike
the os may kill you or it may not. the 10 minutes are more like a 'guideline' but the os may kill you earlier or later anyways.
but CFRunloopRun will run almost indevifinitly (well as long any source is attached or it is stopped). make sure it ends or you never end the bg task.
not forbidden but not cool!
This is a very strange way to setup a background task request. It is unusual to call CFRunLoopRun() manually this way as well. The fact that you do this on the main loop suggests that you're not using the standard UIApplicationMain() function. If not, then you may not be setting up the application correctly, and behaviors can be erratic. Have you tried using background tasks the normal way:
beginBackgroundTaskWithExpirationHandler:
Do the specific task you would like to run even if the program goes into the background
endBackgroundTask: when that task is done
The way you're doing it, endBackgroundTask: is likely never called.
EDIT: If your goal is to be allowed to run after going into the background for as long as the OS is willing to let you run (around 10 minutes), then call beginBackgroundTaskWithExpirationHandler: in applicationWillEnterBackground:. You don't need any special use of the runloop. You just need to tell the OS you'd like to keep going until it stops you.
I'm doing a VOIP project on iPhone. I have the problem to keep the socket alive.
I already configured one socket for VOIP usage, and set kCFStreamNetworkServiceTypeVoIP for CFReadStreamRef and CFWriteStreamRef.
When my app runs in the foreground and I don't do any action, after 1 minute iPhone will sleep, black screen. My socket is still connected to the server.
But when my app runs in the foreground I press home button, and don't do any action, about 1 minute iPhone will sleep, then my socket disconnects from server.
Please help me.
First, have you read Tips for Developing a VoIP App? From your description, it sounds like you haven't set UIBackgroundModes to voip. You also likely will need to configure a keep alive handler.