Silent 3D Touch Quick Action - ios

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.

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.

Is there a way to set an iOS app to automatically download data every night?

We have a customer who wants us to have our iOS app check for new data posted every night and download it if it is available. As far as I can tell this is only possible if the app is already open or if they have someone physically accept a notification or initiate the process themselves.
Is this correct? I can't imagine Apple wanting to allow launching of an app and downloading data with no user interaction at all.
Yes, generally you can only download data at exactly specified times if the app is actually running in the foreground, that is correct. It wouldn't matter if the app was open already or the user opened it from a notification, the point is that user interaction is required. So, the client can't have exactly what they want.
Look at background downloading (fetch) from iOS7+ where you can register the app to perform background downloading in advance of usage. iOS is fully in control of this and it may choose to run the app and it may not. iOS will monitor when your app is usually used by a particular user and, if appropriate, it will allow the app to run in the background to do some downloading before the user is expected to use the app.
In particular you're looking at setMinimumBackgroundFetchInterval: and setting the UIBackgroundModes key with to fetch in the Info.plist.
As an aside, here's an idea based on your expanded information:
Use idleTimerDisabled to prevent the app from sleeping during main usage and keep a track of the time
After the main usage period of the app is over, start your downloads
After the downloads are done allow the screen to sleep
On the next day, if a user opens the app, repeat
It isn't perfect, but it's an approximation

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.

Sending Application to background on iphone

Is it possible to send the application to background programmatically on iPhone?
Under iOS 4.0, your app will be put in the background if another app enters the foreground, and there are enough resources to keep your app in the background. You could send a URL to Safari, or another app that has registered for a handler, and hope that there's enough memory (etc.) that the OS puts your app in the background after it starts Safari (or whatever app handled the URL). If you are lucky (which typically happens fairly often), you will have sent your app programmatically to the background.
Of course, whether your app runs in the background, or is just suspended, depends on other things (which you have to register with iOS 4.x for).
Apple does allow apps to exit (kill themselves) programmatically. But it's meant only as a last resort for when something bad happens from which your app cannot recover.
On all other occasions, apps are expected to stay in the foreground and running until either the user presses the home button or the app opens another app, with the users consent.
Actually you don't want to kill the app, however moving it to the background and returning to the home screen would leave a very similar impression. That's why I don't think Apple would approve it and consequently doesn't offer, as far as I know, any means to move an app to the background programmatically without opening another app.
No it is not possible. Only iOS can put your application in the background.
Note that there's a lot of confusion between "background" and "inactive". An inactive application is one that is not displayed on the GUI but is still running.
Technically an application in the background isn't running; it's dormant. Depending on the mode used, a background applications can receive signals and "wake up." See Background Modes for more details: https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW22
But a background application can also get terminated at any point without warning. As a matter of fact I always treat a background application as terminated. Therefore you should always clean up your application before you enter the background, and re-initialize it when you enter the foreground.
This is a must-read on this topic: https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
check out these post
local notifications?
iPhone - Backgrounding to poll for events

Resources