App background - foreground states - ios

Is there a way i could stop an app from going into background ? Or is there a way in which i could bring my app to foreground if it did go into background ?
I'm making a showcase app for a client and the app must always run on the iPad without interaction from the user.

You can force your app to stay active using following code
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
But if you press home button of device, the app will go in background. You cannot force to stop this.
In this case you can use UILocalNotification to bring the app to the foreground if the device is locked, a notification appears, and the user unlocks the device or if user tap on the notification.
You can also fire a local notification whenever user going to background in
applicationDidEnterBackground: method

Related

Detect when app killed by user in background state iOS

I am working in a chat application where I need to show user status (offline/online).
When my app is in foreground and background then I need to show user as online (managing by VoIP).
But when the user kill the app then it should go to offline.
I have to maintain a flag to show offline which I am managing in delegate function applicationWillTerminate but this function only called when app is in foreground state and user kill it by pressing double tap home button and swipe up.
This function does not get called when app is in background state. I mean simply press home by single tap (app will go in background) then again double tap to swipe up.
Is there any function where I get 100% call either app is in background/foreground state and user kill the app?
Is there any function where I get 100% call either app is in background/foreground state and user kill the app?
No. Just the opposite. If your app is terminated when already in the background, if it is suspended (ie not running in the background due to special entitlement), it is 100% certain you will get no event. You cannot. You are suspended and not running. The app dies in its sleep.
No, As per Apple Document
https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate?language=objc
For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case.
What you can do execute a method(which hit an API for keeping status online) after few seconds(whatever you find suitable time) when you app goes in Background, If method is calling successfully after that specific seconds then user stay online, if its not call after specified second then server update its status to offline. So It require both server and client handling.

How to bring IOS app from background to foreground without using local notification?

Is there any way to bring the app to foreground which is running in background without sending any local notification?
There is no way for you to programatically foreground the app. If you think about it, allowing developers to do that could have significant consequences to user experience.
There must be a way to simulate it.
Uber Driver's App launch when the user gets a new ride with app in background.
WhatsApp in background opens a phone view when somebody calls the user.

How to bring application to foreground in ios?

I am detecting for iBeacon in background. When my device comes in a particular region application should comes to foreground.
It cannot be done without user interaction. The only option is you can generate a push notification to tell the user to bring the application to foreground.
This is from the Apple documentation about this issue:
When the operating system delivers push notification (iOS or OS X)
and the target application is not running in the foreground, it
presents the notification (alert, icon badge number, sound). If there
is a notification alert and the user taps or clicks the action button
(or moves the action slider), the application launches and calls a
method to pass in the local-notification object or remote-notification
payload. If the application is running in the foreground when the
notification is delivered, the application delegate receives a local
or push notification.
To answer to some comments about WhatsApp, with it, when you receive a classic vocal call, IOS use CallKit to display your call and wake up your phone, but it's not inside app. I try to make a video call with WhatsApp, and in this case, there is a notification. Press notification open app and answer to the call.
Conclusion : It's impossible to wake up app from background to foreground in IOS, but it's not really a problem because you can use notification to display what you want and get the user to your app after a touch on your notification. All of iPhone users are familiar with this kind of interaction, it's better to deal with it.

How to detect push notification sent to other apps?

I cannot seem to find a way to detect when a notification comes in. For example, if my app is open and the user gets a Facebook push notification\, is there any UIApplication notification or something else that will tell me that the app is no longer in the foreground and the Facebook alert is on top? Preferably something that is fired if the notification is a banner or an alert
You cannot detect a push notification sent to another app.
If you want to detect whether your app is not in foreground anymore, you can use the applicationWillResignActive: method of UIApplicationDelegate or register for the UIApplicationWillResignActiveNotification notification.
However, when the notification banner appears on top, your application is still in foreground. When the notification is prompted as an alert instead (the user can set this in the preferences), the application loses the focus and it won't be in foreground anymore.
The notification banner at the top is a window displayed by SpringBoard (another process) which does not become key until the user touches it. If the user slides the notification down and the notification center opens, your app becomes inactive and your app delegate hears about this. If the user taps on the notification, your app goes to the background and the other is open. If the user has elected to see alerts instead of banners, once the alert is displayed, your app will resign active. You can listen to UIApplicationWillResignActiveNotification notifications to know when the app resigns active.

detect why iOS app went into background, hardware home or incoming push notification

I want my app to behave differently if the user pushes hardware home button, versus other reasons it may go into background. I think I have sorted out the main cases, except I can't tell the difference between user tapping on incoming push notification from another app and hitting the hardware home. In iOS 5, I was getting an applicationWillResignActive when the notify first appeared, and then applicationDidEnterBackground when the user tapped. In iOS 6, I cannot find any event triggered by the appearance of a banner notification from another app. The applicationWillResignActive and applicationDidEnterBackground come back to back, just as in hitting hardware home. I tried listening to UIWindowDidBecomeVisibleNotification and other UIWindow notification, but nothing is fired. Any ideas?

Resources