Keep Cocoa Window Minimized When App Displays NSAlert - swift

I'm having difficulty determining how to keep an application minimized when it's running and it displays an NSAlert.
The short of it is that I have an application that is transferring data, and it displays an NSAlert when it finishes the transfer. If the app is minimized, I want it to remain that way even though it displays the NSAlert.
I call the NSAlert with (in Swift):
alert.beginSheetModalForWindow(self, completionHandler: handler)
But the window unminimizes itself when this code fires. If the window is already not minimized, it doesn't grab focus or do anything unexpected (which is the way I want it).
Is there a way to accomplish this? Thanks!

You can try to delay displaying NSAlert.
Put your alert code in method. When there is a trigger to call this method check if your window is visible. If yes -> call this method. If not -> schedule an NSTimer that will fire after a second and call custom method to check if window is visible (YES -> display alert, NO -> schedule timer again).
FYI: NSAlert when displayed as sheet creates instance of NSPanel which is a subclass of NSWindow.

Related

Detect application minimize event for UIKIt for Mac?

When i minimize the application applicationDidEnterInBackground not called for Mac Catalyst. After some search i found that Background delegate methods not called for Mac Catalyst but foreground method of Scene delegate called.
is there any way to track or handle minimize delegate methods like NSWindowDelegate have?
Thanks
Well, if you check NSWindowDelegate.windowWillMiniaturize: you'll notice that its parameter is just a notification (its name is NSWindowWillMiniaturizeNotification).
So, you can simply observe that notification to get notified whenever a window in your app is about to be minimized:
NotificationCenter.default.addObserver(forName: NSWindow.willMiniaturizeNotification, object: nil, queue: nil) { notification in
print("This window is about to be minimized:", notification.object)
}
You can also do the same to get notified about other events like:
NSWindow.didMiniaturizeNotification: After the window is minimized.
NSWindow.didDeminiaturizeNotification: After the window is restored/deminimized.
NSWindow.willEnterFullScreenNotification: Before entering full screen.
NSWindow.didEnterFullScreenNotification: After entering full screen.
NSWindow.willExitFullScreenNotification: Before exiting full screen.
NSWindow.didExitFullScreenNotification: After exiting full screen.
Full list of notifications here.

How to determine if app shows the "App X would like to use your current location" alert?

Is there a way to figure out programmatically if CoreLocation is displaying that alert?
I'm displaying a welcome screen and want to adapt it's look if the alert shows up.
When you call the instance method of CLLocationManager -startUpdatingLocation, you can schedule a NSTimer with interval of 1 second for example and inside the timer callback call the -authorizationStatus class method of CLLocationManager. If it returns kCLAuthorizationStatusNotDetermined, then the alert is shown and the user should choose either to allow or deny. If he denies then the -locationManager:didFailWithError delegate method is called with error code kCLErrorDenied and you should stop updating location.

applicationDidEnterBackground & applicationWillResignActive are not being called

I am creating a simple application which perform some task on main thread. I am printing process in NSLog so I can understand that my process is running or not.
Now when I press home button without starting the process (Process will be start when I tap on a button) application enters in background and my both of methods applicationDidEnterBackground & applicationWillResignActive are being called.
But when I first tap on my button and process starts on main thread after that if I press home button none of these two method being called. So my application can't know that app entered in background or not.
Even after that when I again active the app it shows me a black screen with status bar only.
Why this is happening?
Why app not entering in background?
Why apple's methods not being called?
Is there a way to solve it?
UPDATE
Here is my appdelegate class code
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
All methods have no implementation.
Thanks in advance.
I am creating a simple application which perform some task on main thread.
Don't perform long-running operations on the main thread.
The delegate callbacks happen on the main thread. If the main thread is busy, then the callbacks won't happen until you return to the "run loop".
When foregrounding your app, the OS actually displays a screenshot if available, falling back to the launch image (Default.png). The screenshot is taken after -applicationDidEnterBackground: returns, which allows you to customize what gets saved (you might want to do this for security reasons, or to hide UI elements which might not make sense to show when relaunching e.g. a countdown timer).
The black screen is probably because your app has no launch image. If your app takes more than about 10 seconds to enter the background (and it does, since the main thread is blocked), it gets killed. Except the debugger is attached and catches SIGKILL, so it's easy to miss unless you're watching Xcode.
there are some cases
if UIApplicationExitsOnSuspend key set to true in your app's Info.plist, the applicationWillResignActive method is not called when the user hits the home button. and may b some thing other. check keys here Apple keys and see if something new you added to plist. and there is no other case that you say your delegate method not calling. it may also some time due to project in appropriate behavior. try cleaning your project and rebuild.
this is going to sound strange but for those it helps. I had the same issue and cleaned my project and then it started working again.

can any one tell me the name of alert view that should do the works of **C# show dialog**

can any one tell me the name of alert view that should do the works of C# show dialog .
other actions will not work without dismissing it(EVEN DELEGATES ALSO).
Modal dialogs in Windows (in any language) and in iOS are fundamentally different.
On Windows, a modal dialog (and in particular the ShowDialog method) behaves like a function call that will only return when the dialog is closed. Your app will naturally wait until the your has made his decision.
On iOS, presentModalViewController (or [UIAlertView show]) almost immediately return. You can registered a delegate that will be notified when the dialog is closed. But if your app is supposed to wait only the user has chosen something in the dialog, then you have to implement the waiting yourself.
As far as know, You have to do this manually. You can lock your task when the alert is shown and then unlock it again when it is dismissed getting help from these posts.
is there a way to pause an NSTHread indefinitely and have it resumed from another thread?
How to pause an NSThread until notified?

How to terminate process when home button is pressed?

When the iphone home button is pressed, it will only hide the user interface but any process currently running will keep on running at the background. I find that if the process is still running, when i activate the app again, the splash screen will be displayed for a long time until the process is completes before it display the main page of the app. Is there a way to terminate the processes when the home button is pressed?
1) For example, if I have a for loop and before the loop finishes, the home button is pressed, is there a way to terminate the loop?
2) What if instead of a loop, its a delegate waiting for the sdk function to finish the process.
eg. After calling UIImageWriteToSavedPhotosAlbum to save an image, didFinishSavingWithError will be called when it finish saving. Can i terminate the saving process whe the home button is pressed?
Thanks.
You can try adding a new row in the info.plist of your application and select "Application does not run in background" or write "UIApplicationExitsOnSuspend"
Do as Aluminum said.
Not to do 1, but add the following method to the app delegate
(void)applicationDidEnterBackground:(UIApplication *)application
and do something in the method like exit(0)