How to force iOS app to backgorund [duplicate] - ios

I have an iPhone app that I need to send to the background automatically. The app is defined with the VOIP key in its background modes so it should continue running when in background. I specifically need the app to keep running so calling exit(0) is no good.
The app will not be distributed via app store so using a private API is ok.
I have read about UIApplication terminate and UIApplication terminateWithSuccess but they don't seem to be available anymore

Already answered quite well here:
Suspend the application
As that poster wrote:
Quitting your application or sending it to the background programmatically is a violation of the [iOS Human Interface Guidelines][1], which usually doesn't bode well for getting through the review process:
Don’t Quit Programmatically
Never quit an iOS application
programmatically because people tend
to interpret this as a crash. However,
if external circumstances prevent your
application from functioning as
intended, you need to tell your users
about the situation and explain what
they can do about it. Depending on how
severe the application malfunction is,
you have two choices.
Display an attractive screen that describes the problem and suggests a
correction. A screen provides
feedback that reassures users that
there’s nothing wrong with your
application. It puts users in control,
letting them decide whether they want
to take corrective action and continue
using your application or press the
Home button and open a different
application
If only some of your application's features are not working, display
either a screen or an alert when
people activate the feature. Display
the alert only when people try to
access the feature that isn’t
functioning.

In Swift 3 Use below code, working charm
DispatchQueue.main.asyncAfter(deadline: .now()) {
UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
}

While I agree with the other answer that you "shouldn't" exit programatically. There is a way to exit programatically.
*disclaimer - You shouldn't do this.
exit(0);
There is no way to put the application into the background without pressing the home button. If there is, you might want to add the jailbreak flag to your question and ask them.
For more, check this duplicate question, Proper way to exit application.

Related

Monitor app and lock state changes in the background on iOS

I’m trying to make a focus timer app like Forest, that detects if you leave the app, either by going to the home screen, switching to another app or by first locking the phone and later proceeding to a different app through notifications, widgets, camera etc..
What I can’t figure out is how to monitor such state changes even after the phone has been locked for a while. There are no “background modes” covering this use case, so I would expect the app to get suspended after a while in the background. Nevertheless, apps like Forest do this successfully. Do I need a workaround to keep the app awake, or am I missing some approach that doesn’t require background execution at all?
Help much appreciated!
If I had to guess, it's probably a combination of a few things:
Use the normal app delegate callbacks for app state transitions to determine if the app is still able to execute code. That's the easy part.
You could do something with string and regex processing of console logs to detect when other apps open, close, etc. and other activity which would allow you to detect activity on the device coming from places other than your app.
You need to use some sort of framework that Apple says can get data even in background state. One such framework is Core Motion. There may be others as well that suit your app's specific needs better.
See Execution States for Apps.
See Background Execution.
See Cocoanetics: Accessing the iOS System Log.
See Keep iOS App Awake To Monitor Movement.
See Apple System Log Facility.

Perform 3D Touch quick action shortcut without opening the app [duplicate]

As of the new 3D Touch capabilities with the new iPhone 6s/6s+, I'm trying to add some home screen quick actions to my app.
I was able to implement the normal flow of force touching the app's icon in the home screen -> choose one of the quick actions available -> taking care of it properly in all possible app states.
My question is: Is it possible to create a silent action among the available quick actions? By silent I mean that a certain action will take place, yet the app won't complete its launch? Or alternatively launch but won't be in foreground?
UPDATE
I'll elaborate on what I'm trying to achieve - I want to have similar behaviour to the one HealthKit offer with its background delivery - where upon a change in the store, HealthKit wakes my app and give me a chance to do something in the background (with HealthKit example - query for the new data in the store).
After reading much of Apple's documentation on the topic I have the feeling it is not possible with the current API available - but I hope someone will surprise me...
Nope. The user invoking a home screen Quick Action always activates the app.
If your app was already running and is suspended, it comes to the foreground and your app delegate gets the application:performActionForShortcutItem:completionHandler: message. If your app has not been running (i.e. has not been run since install, or was previously backgrounded/suspended but later purged from memory), it launches and your app delegate gets the application:didFinishLaunchingWithOptions: message and then the application:performActionForShortcutItem:completionHandler: message. (So, your did/willFinishLaunching handler needs to check the options dictionary for the possibility of launch via quick action.)
Either way, your app comes to the foreground.

Silent 3D Touch Quick Action

As of the new 3D Touch capabilities with the new iPhone 6s/6s+, I'm trying to add some home screen quick actions to my app.
I was able to implement the normal flow of force touching the app's icon in the home screen -> choose one of the quick actions available -> taking care of it properly in all possible app states.
My question is: Is it possible to create a silent action among the available quick actions? By silent I mean that a certain action will take place, yet the app won't complete its launch? Or alternatively launch but won't be in foreground?
UPDATE
I'll elaborate on what I'm trying to achieve - I want to have similar behaviour to the one HealthKit offer with its background delivery - where upon a change in the store, HealthKit wakes my app and give me a chance to do something in the background (with HealthKit example - query for the new data in the store).
After reading much of Apple's documentation on the topic I have the feeling it is not possible with the current API available - but I hope someone will surprise me...
Nope. The user invoking a home screen Quick Action always activates the app.
If your app was already running and is suspended, it comes to the foreground and your app delegate gets the application:performActionForShortcutItem:completionHandler: message. If your app has not been running (i.e. has not been run since install, or was previously backgrounded/suspended but later purged from memory), it launches and your app delegate gets the application:didFinishLaunchingWithOptions: message and then the application:performActionForShortcutItem:completionHandler: message. (So, your did/willFinishLaunching handler needs to check the options dictionary for the possibility of launch via quick action.)
Either way, your app comes to the foreground.

Programmatically sending an iOS app to background [duplicate]

How can I suspend my application or send a suspend message to my application?
I want to simulate pressing the home button.
There is a private instance method for UIApplication:
The following code would work, (tested in an iPhone 3GS):
UIApplication *app = [UIApplication sharedApplication];
[app performSelector:#selector(suspend)];
In swift:
UIControl().sendAction(#selector(NSURLSessionTask.suspend), to: UIApplication.sharedApplication(), forEvent: nil)
Edit Swift3:
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
Quitting your application or sending it to the background programmatically is a violation of the iOS Human Interface Guidelines, which usually doesn't bode well for getting through the review process:
Don’t Quit Programmatically
Never quit an iOS application
programmatically because people tend
to interpret this as a crash. However,
if external circumstances prevent your
application from functioning as
intended, you need to tell your users
about the situation and explain what
they can do about it. Depending on how
severe the application malfunction is,
you have two choices.
Display an attractive screen that describes the problem and suggests a
correction. A screen provides
feedback that reassures users that
there’s nothing wrong with your
application. It puts users in control,
letting them decide whether they want
to take corrective action and continue
using your application or press the
Home button and open a different
application
If only some of your application's features are not working, display
either a screen or an alert when
people activate the feature. Display
the alert only when people try to
access the feature that isn’t
functioning.
The philosophical reason for this is explained earlier in that document:
People, not applications, should
initiate and control actions. Although
an application can suggest a course of
action or warn about dangerous
consequences, it’s usually a mistake
for the app to take decision-making
away from the user. The best apps find
the correct balance between giving
people the capabilities they need
while helping them avoid dangerous
outcomes.
Users feel more in control of an app
when behaviors and controls are
familiar and predictable. And, when
actions are simple and
straightforward, users can easily
understand and remember them.
People expect to have ample
opportunity to cancel an operation
before it begins, and they expect to
get a chance to confirm their
intention to perform a potentially
destructive action. Finally, people
expect to be able to gracefully stop
an operation that’s underway.
There should be no reason that you need to force your application into the background during its operation. It should remain fully functional when displayed onscreen and it should be up to the user when they want to switch away from your application.
You can't do this.
If you want to exit your app, don't fear rejection and love false positive crash report emails from users you could call exit(0);
Use :
sleep(15)
it will suspend your app for specific time
When the application is suspended, a UIApplicationWillResignActiveNotification notification is posted.
I would guess you could try to post this notification manually. Didn't try this myself though.

Is there any way to programmatically send my iPhone app to the background

I have an iPhone app that I need to send to the background automatically. The app is defined with the VOIP key in its background modes so it should continue running when in background. I specifically need the app to keep running so calling exit(0) is no good.
The app will not be distributed via app store so using a private API is ok.
I have read about UIApplication terminate and UIApplication terminateWithSuccess but they don't seem to be available anymore
Already answered quite well here:
Suspend the application
As that poster wrote:
Quitting your application or sending it to the background programmatically is a violation of the [iOS Human Interface Guidelines][1], which usually doesn't bode well for getting through the review process:
Don’t Quit Programmatically
Never quit an iOS application
programmatically because people tend
to interpret this as a crash. However,
if external circumstances prevent your
application from functioning as
intended, you need to tell your users
about the situation and explain what
they can do about it. Depending on how
severe the application malfunction is,
you have two choices.
Display an attractive screen that describes the problem and suggests a
correction. A screen provides
feedback that reassures users that
there’s nothing wrong with your
application. It puts users in control,
letting them decide whether they want
to take corrective action and continue
using your application or press the
Home button and open a different
application
If only some of your application's features are not working, display
either a screen or an alert when
people activate the feature. Display
the alert only when people try to
access the feature that isn’t
functioning.
In Swift 3 Use below code, working charm
DispatchQueue.main.asyncAfter(deadline: .now()) {
UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
}
While I agree with the other answer that you "shouldn't" exit programatically. There is a way to exit programatically.
*disclaimer - You shouldn't do this.
exit(0);
There is no way to put the application into the background without pressing the home button. If there is, you might want to add the jailbreak flag to your question and ask them.
For more, check this duplicate question, Proper way to exit application.

Resources