Can peripheral advertisement/broadcasting be reactivated in the background? - ios7

From what I understand, once an app is terminated by iOS for memory use, my app will stop advertising as a peripheral (even if I've enabled the peripheral in the background).
Is it possible to reactivate the peripheral advertisement when my app is woken in the background for a periodic server update, or in the case of using Significant Location Changes where the app is woken in the background every time the phone moves to a new location?

Related

Background Location Services after reboot in iOS 7

I'm interested in a border line iPhone app able to detect the user location continuosly.
The app will be delivered in enterprise mode (so no Apple approval).
Basically it should run forever (resetting the backgroundTimeRemaining through the location service start end stop).
the issue that I was not able to fix is to relaunch the app after a reboot, event if I declare the voip capability, it seems that the system give me only 3 minutes, no more time despite all the workarounds.
so actually I'd like to know if someone was able to implement this sort of behaviour on iOS7.
thanks in advances, please find below some references.
iPhone - Backgrounding to poll for events
to run app continuously in the background
Background Location Services not working in iOS 7
Start Location Manager in iOS 7 from background task
ok, I got it.
the music hack is the solution.
During the further 180s of background time a small mute playback of few seconds (of local mp3) restart the backgroundTimeRemaining counter.
bye

Background modes are turned off automatically

I have set in the Info.plist, the "app require background modes", the "app registers for location updates".
I put the app to be in the background, and I do get the location delegates for a while, but after 25 minutes it stopped getting the locations.
How do I know that? Each call of the delegate, it sends the data to the server (parse) .
I am also wondering if the problem arises because the location is turned off in the background, or because I can't send data to the server at background.
Also do I have to turn on audio to keep it alive?
According to Apple, I can listen in the background with:
apps that play audible content to the user while in the background, such as a music player app
apps that record audio content while in the background
apps that keep users informed of their location at all times, such as a navigation app
apps that support Voice over Internet Protocol (VoIP)
apps that need to download and process new content regularly
apps that receive regular updates from external accessories
Do I have to turn on an audio? Our app needs to get data location constantly from business.
This issue is amazing. There are dozens of opinions about what you can do and what you can't do. There is no one place that tells you how you can sample background locations, and if Apple allows this.
I guess problem is with pausesLocationUpdatesAutomaticallyBy
By default LocationManager pausesLocationUpdatesAutomatically is Yes, so after some if your device is not move or not change location may OS will stop the app, so set NO for this property , and not need to turn on an audio.

CMMotionActivityManager - Receiving motion activity updates while app is suspended or in background

I've been testing the new Motion Activity Manager APIs (CMMotionActivityManager). I want to create an app that can receive and process motion updates from startActivityUpdatesToQueue while the app is not running in the foreground. I correctly receive motion updates (stationary, walking, running, automotive, unknown) while the app is in the foreground but as soon as it goes into the background (and thus gets suspended), motion updates cease to be received by the app.
CMMotionActivityManager Class Reference:
"This method initiates the tracking of motion data asynchronously. Upon calling this method, the motion activity manager executes the handler block on the specified queue, reporting the current motion in effect for the device. After that, the motion activity manager executes the handler block only when the motion data changes.
The handler block is executed on a best effort basis and updates are not delivered while your app is suspended. If updates arrived while your app was suspended, the last update is delivered to your app when it resumes execution. To get all of the updates that occurred while your app was suspended, use the queryActivityStartingFromDate:toDate:toQueue:withHandler: method."
I want a way to keep my app running in the background and receive motion data (real-time). Right now the only way to keep the app running in the background is to use background services but "motion updates" is not in the list of allowed background services:
App Store Review Guidelines:
"2.5.4 Multitasking Apps may only use background services for their intended purposes: VoIP, audio playback, location, task completion, local notifications, etc."
I don't want to have Location Updates (GPS) on all the time (to save battery). If I bank on continuous location updates but switch the GPS off, the app will get suspended.
Please let me know how to keep my app running in the background and receive motion activity updates.
There must be a way to do it: I tried the Strava app....even when the app is in the background, it swithes the GPS off when the user pauses running. When the user resumes running, it automatically turns the GPS on (all this while the app is in the background)...this probably means that the app is running in the background and actively receiving motion activity updates even while the GPS is off.
This is not a new issue. There has never been a Core Motion background usage setting. You are allowed to use Core Motion if your app is running in the background, but to make it run in the background you have to be doing something else (e.g. getting location updates at the time the user sends the app into the background, and you have the location background service listed in your Info.plist.
Note too that in iOS 7 if you have the right hardware you may be able to use deferred location updates to wake your app periodically in the background. Thus you save power but your app gets to run in the background now and then without an Info.plist background service listing.

Confusion between background vs suspended app states

I am bit confused about these two states. Following is my understanding;
when app is in background and if you have "Application does not run
in background" set to NO in App plist file then App continues running
in background. In suspend mode App stays in memory but does not
execute code! System doesn't notify App when it moves to Suspended
state and wipes out App from memory in low memory situation to create space for foreground Apps.
Is my understanding correct? If it is so then why don't I get my NSLog printed on console when App running in background and executing code? What actually happens to my TCP socket connection where I am continuously gets the data from server? Why do we need Suspended state as anyway System eventually wipes out App from memory in low memory situation. Is there any difference between App becoming active from suspended state or starting fresh?
You should look in the IOS App Programming Guide section "App States and Multitasking".
Find that by searching the Xcode documentation with the phrase "App States and Multitasking" and "Hits Must" item set to "Match Search Term". Another useful search phrase: "background execution".
The summary answer is that an application can continue "executing" in the background indefinitely for only a limited number of reasons:
in iOS, only specific app types are allowed to run in the background:
Apps that play audible content to the user while in the background,
such as a music player app
Apps that keep users informed of their
location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Newsstand apps that need to download and process new content
Apps that receive regular updates from external accessories
Aside from those specific operations, an app can ask to continue to execute for a very short time which the documentation covers in the section "Executing a Finite-Length Task in the Background". After a short time either your app tells the system it is done (and is then suspended) or it is forcibly terminated. Details in the doc.
Another useful bit of that document, with nice state diagrams, is the section "Managing App State Changes". That section talks about going into the background and returning to the foreground. It should answer your question about the difference between starting fresh versus starting from the suspended state. The short (not quite correct) answer is that if you start from the suspended state and you don't take any special actions when entering the background or (re)entering the foreground then you just continue more-or-less from where you were. Also, starting from the suspended state is faster. Read the doc since it says it much better than my paraphrase would.
App State
Not running: Your app is in this state before it starts up.
Active: Once your app is started, receiving events.
Inactive: When your app is running but something happens to interrupt it, like a phone call, it becomes inactive. Inactive means that the app is still running in the foreground but it’s not receiving events.
Backgrounded: In this state, your app is not in the foreground anymore but it is still able to run code.
Suspended: Your app enters this state when it’s no longer able to run code.
Background:
Executing code - Code is executed while the app is in a background state.
An app must go into a background state before it can go into a suspended state.
ie. Suppose you're on Facebook and you upload a video and you quickly switch to another app immediately after pressing the POST button. Although you switched to another app w/o terminating the app, the app can be configured to perform background processes to complete the upload.
An app that is in background does not necessarily mean it is suspended but an app that is suspended is in background.
An app can request to stay in background mode for extra time (for purposes of playing audio in the background or completing a network request, for example); afterward it will either go to suspended state or it will be forcibly terminated by the system.
Suspended:
Not executing code - Code is not executed while the app is in a suspended state.
An app that is in a suspended state is also in a background state.
The system moves apps to this state w/o notifying you beforehand.
An app in a suspended state is still in memory.
Some additional information that may help you on this topic:
UIApplication notifies the app of state transitions through methods in the AppDelegate.
Most state transitions are accompanies by these methods.
These methods are part of the UIApplicationDelegate protocol.
They provide you with a chance to respond to state changes.
The method below, for example, tells the delegate when the app is about to terminate. - It's only called if the app's in background before being terminated; not suspended.
func applicationWillTerminate(_ application: UIApplication) { ... }
In addition to Charlie Price's answer from 2013, iOS now also allows background for:
REMOTE NOTIFICATIONS that signal there is new content available for download. When a remote notification arrives, the system launches or resumes the app in the background and gives it a small amount of time to download the new content.
CORE BLUETOOTH to communicate with a bluetooth accessory while in background.
Click here for more details
UIApplication.shared.backgroundTimeRemaining tells you how many seconds is remaining until the system terminates the application. 3 minutes is the maximum time limit, but this time restarts each time the app is woken up in background mode.
You can prevent your app from ever entering a background or suspended state & instead immediately terminating by setting the UIApplicationExitsOnSuspend key to YES in the info.plist.
Additional links you may find useful:
https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
https://developer.apple.com/documentation/uikit/core_app/managing_your_app_s_life_cycle/preparing_your_app_to_run_in_the_background/about_the_background_execution_sequence
https://medium.com/#abhimuralidharan/finite-length-tasks-in-background-ios-swift-60f2db4fa01b
https://qph.fs.quoracdn.net/main-qimg-473264d5d9f0ec16d57b8dffcc9824d8

Android issues in wakelock?

Hai i developed a application using broadcast receiver.I need to wake my Application always.so i used wakelock.here my code
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
problem
1.If i wake up always my battery is getting drained.
2.if i cal w1.acquier() ,Is necessary to release the w1(ie w1.release)
I don't understand what is your 1st question.. I thought that is a fact if you want to keep waking up your application?
If keep your screen not dimming down is what you want to achieve, you could try this code:
//This is to keep the screen not dimming down.
Window.AddFlags(WindowManagerFlags.KeepScreenOn);
Wake lock keeps the cpu running on your application even when the screen is off so it will drain your battery hard. You want to call release on your wake lock when you no longer need the application to keep running in the background (example not waiting for any more callbacks).