wake up app after terminate - ios

i want to wake up my app after app terminated using background modes...
i know using location update and push notifications we can do.but i don't to use those. apart from those is their any way to wakeup my app.
actually i need to connect my app with websocket even app was terminated.
is their any way to wakeup app using core motions. or using microphone(i mean if app catches any data of voice(sound))..
can any one explain app life cycle (when it will wakeup and when it will sleep)
thanks u

Even if there is a way (actually there are some tricks with beacons but user would have to be in the range of beacon specified by you) it shouldn't be used like so.
If your app is kind of weather service or newsfeed, iOS device will be woken up at intervals specified by you (not less than 1h) to check for necessary data.
Using microphone or core motion to wake up your app probably won't pass apple review.
To fully answer your question I would have to know reason for background mode.

According to your requirement "i need to track device motion activity", you could use the queryActivityStarting() provided by apple API's.
This gathers and returns historical motion data for the specified time period:
let activityManager = CMMotionActivityManager()
activityManager.queryActivityStarting(from: lastTimeAppCollectedData, to: now, to: queue) { (activities, error) in}
This returns you an array of activities/error that happened in the given period
It still wont wake your app up, but will allow you to query the events after they happened.

You can use CLRegion for geofencing as soon as user exit the region boundary, your app will get open with didExit delegate of CLRegion and after that you can use startActivityUpdates of CMMotionActivityManager to keep your app open till you want.
Resources:
Region Monitoring

No once app is terminated you don't have any access to app until app is opened by user. Even location update and push notifications only work in background.

Related

How to BLE device will invoke the app, when app has terminated

I have a one BLE Device(Peripheral) and iOS Application which communicate to each other using CoreBluetooth.framework( connect, disconnect, subscribe, notify services). Here are the few scenario:
The app is foreground -> I am very easily connected with BLE device.
The app is in the background -> I get the callback and connect with the BLE device
The app is removed from the background (Kill the app) -> I am not getting any callback even I connect the device from iPhone Device-> Settings-> Bluetooth on -> Select Ble Device -> Connect.
I google and get State Preservation and Restoration will be invoked the app. After deep drive :
This also showing App Force Quit by the user , app will not relaunch or active using state preservation and restoration.
I read from this, this and this, have some points :
"If you need to execute code when your app isn’t running, there are several options open to you depending on what you’re trying to do.
- Background fetch will let your app run in the background for about 30 seconds at scheduled intervals. The goal of this is to fetch data and prepare your UI for when the app runs next.
- Push notifications let your app fetch fresh data from your server. You can make a message appear on the device if you want, but it’s not required – silent push notifications let you skip that part.
- Local notifications let you display an alert to the user, along with any media attachments you want and some options for the user to select from. If they choose those options then your app can be launched in the foreground or background to handle them."
I tried with Background Fetch, but it's also not awake when the application has been terminated.
My single objective to achieve is "When the application has terminated or killed by the user, Whenever BLE Device is connected the app should be invoked in foreground/background so that I will perform some operation like get the data from BLE Device and save it"
Using background modes in a project:
Other BackgroundModes also not gives any clue:
How can achieve this without using push notification or silent notification?
if anything in CoreBluetooth framework where the application awake after the termination, let me know?
The short answer is, you can't.
The documentation clearly states that your app will not be relaunched when it is explicitly killed by the user.
This is true for silent notifications as well - these will not wake your app up if the user had forced terminated your app (or the device battery state is below 20%, btw).
Your options for this are limited, and may include advising the user not to force kill your app, or using location based region detection to re-launch your app.
One of the tutorials you've linked shows an iBeacon example to detect going in and out of range of iBeacons, and when combined with background permission can relaunch your app, but again - this isn't explicitly what you've asked for and is not a real solution to the problem you describe.
Apple's logic is simple - if the user kills your app, the user doesn't want it to run again, which makes allot of sense.
The thing is, many users think that killing apps is the standard way to make their device more responsive, there's a big debate on this (what's worse? having the OS cold start apps or allowing apps to do funny stuff in the background with silent notifications, etc.) I won't go into that debate, that's just the way it is.
This is Abstract code to highlight the solution. You might need to tweak few things.
1> Create CLBeaconRegion with UUID start monitoring for that beacon
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
if let uuid = UUID(uuidString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D") {
let beaconRegion = CLBeaconRegion(proximityUUID: uuid,identifier:"iBeacon")
beaconRegion.notifyOnExit = true
beaconRegion.notifyOnEntry = true
locationManager.startMonitoring(for: beaconRegion)
}
2> As per the document
If you begin monitoring a region and your app is subsequently
terminated, the system automatically relaunches it into the background
if the region boundary is crossed
Reference
3> Your peripheral should transmit as ibeacon who transmit same UUID (Same UUID for which you have created CLBeaconRegion)
You might need to play with few parameters of CLLocationManager and CLBeaconRegion but this should work in my opinion because in the past my apps have been successfully woken up from terminated state on iBeacon detection ....

Background Bluetooth with iOS device

I am working on developing a bluetooth peripheral to work with my iOS device. I need to make the iOS app receive data whilst it's in background and process that data as it comes. Looking through Apple's CoreBluetooth framework, I can see how the background execution modes can be used. Now to save power, I want the iOS device to only connect to the bluetooth peripheral at a certain time (without need of user interaction). I've looked through Local Notifications on iOS and it has very limited functionality and don't think it provides what I need.
So is there anyway to wake up an app at 6pm and ask the application to start scanning for bluetooth devices? And then execute other code once device is connected? All this without user interaction.
Any suggestions would be appreciated!
Thanks!
You can't really schedule operations to occur at a specific time in iOS (aside from local notification, which as you said isn't what you need).
You can use background fetch mode to periodically allow your app to check for new data. You can set an interval (although this is only a guideline to iOS, not a strict schedule) for how often your app is woken.
When iOS calls your app delegate performFetchWithCompletionHandler method you can check the current time and decide whether you want to transfer data. If not then you can quickly return UIBackgroundFetchResultNoData. If you do get new data then you can retrieve it before returning UIBackgroundFetchResultNewData

Launch my app from bluetooth iBeacon connection (Private API)

I am creating a private distribution app and I am wondering if it is possible, using any methods or private API's, to open my app when a bluetooth connection has been made.
What I have discovered so far is that with iOS 7 and the ability to use iBeacons you can enter into bluetooth proximity and you can have your app send a notification to allow the user to then open the app.
What I am hoping to do then is have the app running in the background and listen for an iBeacon connection and, if one has been made, actually launch the app without the users control instead of just sending a notification.
I realize this would never be allowed publically, however is there anything private I can look at to achieve this without jailbreaking? I know to launch some apps you can do [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; however I want my app to listen for an iBeacon and then open itself up.
Is there any way or work around to achieve this?
Moving app from background to foreground without user interaction is pain in the ass. Sorry, don't have a solution, just want to share some information:
I asked the same question here and posted a bounty on it and got no good response:
Show some UI from background in audio player or VOIP app on iOS
At some moment I found a solution with the help of another person. It was based on usage of GSEvent (sending clicks to UI). You can look following questions. However, as I know, in iOS 7 these API became protected by entitlement. So, this method is dead (most likely).
Using GraphicsServices.h/GSEvent as well as compiling CLI iPhone tools with Xcode
Use GSEvent to send touch event,but it's invalid.
Simulating System Wide Touch Events on iOS
iPhone, how to fire a button event in programmatically
Apps can use region monitoring to be notified when the user crosses geographic boundaries or when the user enters or exits the vicinity of a beacon. While a beacon is in range of the user’s device, apps can also monitor for the relative distance to the beacon.
In iOS, regions associated with your app are tracked at all times, including when your app is not running. If a region boundary is crossed while an app is not running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it is woken up and given a short amount of time (around 10 seconds) to handle the event. When necessary, an app can request more background execution time.
For detailed info:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html#//apple_ref/doc/uid/TP40009497-CH9-SW1
There are two scenarios: you either want users to be able to do something with your device other then use your app, or you want them to always be locked into your app.
In the former case you should trust user. Just show the push, timer or location notification and let them decide to launch the app or not.
In the latter case just lock the device using guided access mode.

Push Notifications and Services for iOS

I have an app which is tracking the coordinates of the user. I want the user to receive a push notification at a specific longitude and latitude. That means that my device has to track coordinates and make requests even if it's closed.
Is there any way to accomplish this?
Push Notifications can't do this.
CLLocationManager actually has a method, startMonitoringForRegion:desiredAccuracy: to monitor for when a users enters a region and wake your app if needed. See more here. I'm pretty sure this is the system they use for their Reminders app, where you can get a reminder at a specific location. It uses a lot less energy than having your app monitor the location all the time, since the OS can use information like which cell towers or wifi hotspots you are nearby, to figure out if it's even necessary to use the GPS.
Take a look at the document and this
Your app will keep running in the background and receive location updates.Then send local notification to the user. But if the user killed your app manually.The work will not been done.

Running IOS App with Screen Locked

I am working on a sports app that uses GPS and OpenEars text-to-speech. The app delivers speed and other GPS info to the user audibly (via earphones) so the user (skier, cyclist, etc) can get the GPS info without looking at the screen. In order to save battery life, I would like the app to run in the background with the screen off. Is this possible with IOS? I tried setting "Required backgrund modes" for both GPS and audio, but my app quits working when the screen is off.
thanks..
One one to get around it is to have a silent audio file playing, when there is no audio, it might get your app rejected in the app store though... here is a question that talks about the silent audio file playing to keep in the background... It might get through app store approval in your situation since it is an app that relays on audio as one of its main functionalities..
You can subscribe to GPS info change notifications and your app will run when something needs to be updated. Check this.
Please note this sentence:
"If you leave this service running and your application is subsequently suspended or terminated, the service automatically wakes up your application when new location data arrives. At wake-up time, your application is put into the background and given a small amount of time to process the location data. Because your application is in the background, it should do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. If it does not, your application may be terminated."
There's no way to do it as service except some special cases provided by ios. But you can simulate it. This post answer how to:
https://stackoverflow.com/a/19121250/2835520
I hope it's gonna help!

Resources