I'm trying to get state restoration working from SwiftUI. SceneStorage works, but I want to have more control over the process, both so I can disable it for tests and so I can control it from a coordinator separate from my views.
userActivity(_:isActive:_:) gets called when the app opens, but not when the app closes. How can I get it to get called to update the apps state? onContinueUserActivity(_:perform:) on the other hand doesn't get called at all.
What am I missing here?
Related
I'm trying to find where to put app initialization code that should NOT be run in preview mode. I've seen several answers suggesting it's incorrect to split the app behaviour this way (launch vs preview), but I disagree: many apps need to do additional setup (eg connect to database, launch background tasks, call APIs, etc) that isn't appropriate for the preview (where static test data makes most sense).
In preview mode, Xcode actually runs the app and calls AppDelegate.applicationDidFinishLaunching, so any post-launch initialization code there will be triggered.
What is the recommended way to run app setup code so that it doesn't run in preview?
It appears that Xcode sets an environment variable when running the app for SwiftUI previews. The key is "XCODE_RUNNING_FOR_PREVIEWS", which will have a value of "1".
Given this I found putting a guard statement that checks that environment value in my applicationDidFinishLaunching implementation before the initialization I didn't want to occur for previews fixed my preview (my initialization was making them fail entirely).
I also wrapped it in a DEBUG check to ensure it would not ever accidentally break a production build.
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Initialization needed for previews
#if DEBUG
guard ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != "1" else {
return
}
#endif
// Further initialization not needed for previews
}
The Preview Live, I assume you mean it, creates complete window scene context and injects there view to be previewed, so all application & scene delegate methods are called including instantiating root ContentView, but the root view is not shown, ie. its body is not called.
Thus you can achieve your goal by placing code, initiating all heavy/network/etc. operations in root view's .onAppear callback. And this, actually, will be good for your users as well, because such approach gives fast application start and initial UI presented.
TL;DR:
Workaround this issue by putting your SwiftUI code in a framework and make it the selected scheme in Xcode before viewing your previews.
Details:
Looks like SwiftUI Previews need to build a target that contains the SwiftUI code (makes sense), but that target doesn't have to be an app! So it's unclear why it needs to run the app at all, but clearly it does. Xcode picks which target to build from the currently selected scheme (if you pick a target that doesn't build the required SwiftUI files, you'll get an error in the previews). So here's a workaround:
Add a new framework target
Put your view code in that new target
Select the scheme for that new target
Run your preview
This also has the advantage of only compiling the new target (and its dependencies) for rendering previews, which is likely to be faster that building the whole app.
Any dependencies will of course need to be accessible from that new target.
This works at least in Xcode 12.5.1
I have an MFC MDI application, in which it is possible for documents to have multiple views, and for users to customise and then save layout data for the views. This data is associated with the views, not the documents.
I would like to prompt the users to save if they choose to close a view with unsaved layout changes, and am running into problems, as it seems MFC is only geared towards changes in the document. Here are some approaches I've tried:
Override CDocument::SaveModified function, which gets called by the framework when a document is closed. In this function, I send a message to all the document's views, which can then check for unsaved changes and prompt the user.
Perform the check inside the View's destructor.
Perform the check inside the View's OnClose handler
Each of these approaches has problems. (1) is the best, but it fails to deal with cases where there are several views onto one document, and the user closes one of the views. As the document is still open, SaveModified is not called.
The problem with (2) is that on application shutdown, the application has already disappeard by the time any CView destrutors are called. This can leave an orphan dialog box open on the desktop. This is also the case if I try performing the check inside OnDestroy.
I can't get (3) to work - I can't get my views to respond to WM_CLOSE.
Currently, my best solution is to do both (1) and (2), but this requires some smelly logic to prevent the app from prompting the user to save view changes twice on app shutdown.
Anyone know of a better way of doing this? Where is the correct place to hook in?
I'm not sure if it is your solution, but I have several views that can not close on condition and I handle them in DestroyWindow( ). And a message box there does come up over the app before it closes down. So try using DestroyWindow( ) rather than the destructor.
Concur.
ON_WM_DESTROY()
afx_msg void OnDestroy();
does the trick. It will not prevent the window from closing, but the question didn't request it.
start menu's item can be executed single-click,
explorer's item can be executed double-click or right-click and (O)pen.
like this, Windows UI is many execution method.
I want to know execution through UI(like item double-click).
How to know that?
I try to use UI Automation. but focus change is Too late and sometimes executed without focus change.
so I want certain method.
The assumption.
UI to kernel execution message.
Can I get it?
And other method is exist?
I have a dialog based MFC application and I want to get notified when the complete application becomes active or inactive. When the user switches to another application and comes back to my application I need to execute some code.
How can I do this?
I already tried OnActivate but that doesn't help me much. The main window will also becomes inactive when another window of the same application will open. That is not what I need.
You receive WM_ACTIVATEAPP when another window gets active that doesn't belong to your application.
I just installed your framework successfully to my cocos2d-v3 game project. My game has short runs and I want the player to be able to share he’s single runs.
So I call startRecording when my run starts.
[[[Everyplay sharedInstance] capture] startRecording];
And stopRecording when it ends.
[[[Everyplay sharedInstance] capture] stopRecording];
But when I call stopRecording it automatically opens the video modal and I don’t want that. I want to show a replay button and when player presses the button I will call playLastRecording-method. Am I doing something wrong or why stopRecording-method opens the modal automatically?
Maybe you have copied the everyplayRecordingStopped example implementation from the Everyplay integration guide without reading the code?
https://developers.everyplay.com/documentation/Everyplay-integration-to-Cocos2d-game.md