Is there a way of restarting an app when coming back from background? - objective-c

I want to be able to restart the app when coming back from background. So if the user selects the app again it should start as if it were the first time it's open. I've been googling but couldn't find a way of doing this.
I was thinking in just add the main view of the app in applicationWillEnterForeground, but It would be great if I can deallocate resources.

You can't restart an app. What you can do is disable background support, so your app always completely terminates when closing.
"...you can explicitly opt out of the
background execution model by adding
the UIApplicationExitsOnSuspend key to
your application’s Info.plist file and
setting its value to YES."
Source: Opting Out of Background Execution.

Related

How to detect when an app is launched on macOS?

I have a background process running on the user’s macOS machine. Its job is to detect whenever any app is launched on the machine. Currently, I am detecting the NSNotificationCenter’s NSWorkspaceDidLaunchApplicationNotification event. This works perfectly for detecting when an app is freshly launched (i.e. the app had no instance already running at that time).
But, on macOS, if we click the red cross button at the top-left corner, it generally closes the app window and the app continues to run in the background. This is also evident by the app icon visible on the dock with the dot indicator below it. If I click the app icon on the dock and then launch it, the NSWorkspaceDidLaunchApplicationNotification event won’t be triggered.
To track such events, I tried using the NSWorkspaceDidActivateApplicationNotification event. Using this event, I was able to detect all the app launch scenarios. The problem is that this event gets triggered whenever the app comes into focus such as switching windows using command+tab, clicking on its dock icon, changing between two apps, …
Is there a way to filter out these triggers or identify which action led to the trigger? Or is there some other event/ method I can listen to which gives the required filtered triggers? I only want to detect scenarios where a new window of the app is created.
What you seems to want is two different things, as was mentioned in comments, which should be processed separately to be reached.
To detect app launch, when the new process is started. You could use the NSWorkspaceDidLaunchApplicationNotification if it is enough (usually for visual user apps), or kqueue if it is not, or even EndpointSecurity framework to rule them all.
To track the window(s) in the already launched app. Visually, if the white dot under app dock icon is there, the app is still launched.
There is an Accessibility framework for this task, you could track the event of window creation, window destruction, get count of windows from target process id, visibility state and etc.
It is a bit abandoned and has no updates since maybe release, but it will work for you in most cases.

How to restart application after sometime when we minimize it from home button and open it again

I have created Mobile application in React native.
My problem is that whenever i open my app from launcher icon and minimize for sometime (eg: 10 minutes) then if i re-open my application from recent apps or else on clicking launcher icon again it should restart the application as it opening for the first time.
But doesn't matter how much time i minimize the application when i open it again it will open as a previous state.
How to solve this issue.
What Navigation module are you using? Depending on it, you need to call a method to init the app and that method must be called again when you switch back to the app.
Normally, application has their inherent state as snapshot when it go to background if the user do not close directly.
If you are testing on android, pressing backbutton on top of the navigation makes close the application. (you can control this if you want.)
In iOS, pressing home button always send the application into background instead of close.
I am not sure what OS you are using for development. But refer it.

Keep my app as responder while calling activateWithOptions:NSApplicationActivateIgnoringOtherApps

I am making a vim-style "window manager" that takes text input, much like Alfred or Spotlight in Mavericks (in a floating panel).
The problem I'm having is when I call activateWithOptions: on a running application it steals focus from my window. I was hoping the problem would be solved by simply bring my app to the foreground again, however it seems the activation is running on a separate thread, and I end up activating my app before the original app gets activated.
I have tried reactivating when I receive NSWorkspaceDidActivateApplicationNotification, but that doesn't work either.
Ideally I'd like to pause execution until the application is focused for multiple reasons, since that would be the window I manipulate further.
Does anyone have any suggestions?

Detect screen shutdown in vb.net

I'm building an application using vb.net that should record some specific user actions, one of these actions is the screen shutdown, is there a way to do that? Maybe a system event or something that i can check periodically to know if the screen is active or not.
I've already found some code to check if the screensaver is running but i want to know if there's a way to detect if the screen is phisically turned off.

NSAlertPanel problems

I am designing an application to connect remotely to another computer. I want to display an NSAlertPanel on connecting however it is 'blocking' the remote side from continuing with the session until OK is pressed with the usual NSAlertPanel setup.
Is there a way to have an NSAlertPanel which is non-blocking? Thanks.
When you run your alert panel modally, you block the run loop of the associated thread, which is the main thread in this case.
To display a window on connection, you can use custom sheets. It's easy to use and explained in the documentation page below:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Sheets/Tasks/UsingCustomSheets.html
However, if you need to run your alert modally, an alert that is blocking interactions with the whole application, you may need to move your connection part to another thread, which is a good practice in both cases.