iOS 14 widget UserDefaults(suiteName:) not working - swiftui

Xcode 12.0 (12A7208)
MacOS 10.15.6 (19G2021)
Create an new SwiftUI project.
Add Capability App Groups.
Create an new Target Widget Extension, add Capability App Groups.
In Main App use UserDefaults(suiteName:).set to save an Int data.
In Widget, Under getTimeline(for:in:completion:) function just call UserDefaults(suiteName:).value,cannot get the Int Data. But in Main App can.
suiteName is started with group., and it is same in Widget and Main App.
quite confused...

For any future readers I found this error - "Code Signing Error - Verify the value of the CODE_SIGN_ENTITLEMENTS" which was in a very un-obvious place.
To fix:
Go to your new Target
Go to Build Settings
Go to the Signing section. Make sure you have an .entitlement file assigned to both Release and Debug. An entitlement file should've been automatically created in your folder structure. You just need to make sure you get the path correct eg. "Widget/WidgetExtension.entitlement"

Related

SwiftUI: where to put app initialization code so it doesn't run in preview?

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

Qt Disable Windows 10 Game Bar

I have an application developed in Qt that causes Windows 10 to think it is a game, and opens a pop up box that says Press the Win-key + G to open the Game bar. This is very unhelpful as my application is not a game; and it interferes with the user experience. How can I turn this off from within my application code? I have been unable to find any documentation related to this. Thanks in advance.
It is not possible to neither capture Windows shortcuts (in order to stop propagation and disable them), nor to disable game bar in a per-app base.
Options you have are:
to disable it globally (see this post): you can do it using the Registry, so it can be included in an installation package, but you'll affect the global settings of the user,
change the shortcut used to access it in the Xbox app,
use some third-party app, such as AutoHotKey, to map keyboard sequences to an empty action (related question).
Edit:
Also you can (from user side) disable it for your app in Xbox app. (Xbox support):
Open Xbox app
In my games list select your app
Right click on it and delete it
This will delete your app from games list, so GameBar will not appear.

Make a window stay on top and be only active window using QT/C++

I am on RHEL 6 and am creating an app using C++ and QT. I am trying to make my window/app be on top and be the only app the user can interact with.
I have set:
setWindowFlags(Qt::WindowStaysOnTopHint);
But the user can still click on a different app, which then comes to the top. I need to prevent that.
I was able to fix it by adding "show();" after I set the windowFlags.

Qt Creator Session Manager Bug?

The default session works as expected, but when creating a new session, neither the projects nor the editors are restored. The current projects and editors are closed after ‘switch to,’ but nothing is loaded and I am left with blank editor windows (laid out as they were before initiating the switch) and no projects loaded. Any suggestions on how to further debug or otherwise resolve this issue will be greatly appreciated.
(Using Qt Creator 3.2.2 on Windows 8.1)
(I also created a new session called ‘xxx’ and grepped for ‘xxx’ in my project directory as well as AppData/Roaming and AppData/Local and all subdirectories thereof, and did not find the string anywhere.)
I am able to successfully use sessions now. I had previously expected “New” to create a new session with the current state, but I see now that it does not work that way.
If I understand correctly now, the current state is tracked by the currently open session, and saved on switch. Therefore, when one switches to another session, the state of the GUI just before switching is available in the session from which one switched.
A new session is blank, so if a new session is created and then one switches to it, there will be nothing available, so no projects or editors will be loaded and the window arrangement will remain in the same layout as it was before switching (but with empty editors).
To create a new session with the current state, use clone.

Adding an item to Internet Explorer's right-click context menu

I'm trying to add a new entry into Internet Explorer's right-click context menu. I understand that this can be achieved by creating an HTML file containing JavaScript, and then linking to this from a location in the registry. I have also read that you can also add the HTML to a resource file and compile it into a DLL (see the Microsoft KB: Adding Entries to the Standard Context Menu). This is where I have started to hit problems.
Here is a bit of background about what I have done so far.
I have the following JavaScript in the file C:\test.htm:
<script type="text/javascript">
alert('Hello, world!');
</script>
I have added a new REG_SZ value 'c:\test.htm' in the registry at the following location:
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt
If I now restart IE, my new menu item appears in the context menu. If I select my new menu item, my message box alert appears as expected. So far so good. However, I can't seem to access the script if it's in a DLL. Here are the steps I have taken:
Created a new Visual C++ Class Library project in VS 2005 named 'IETest' in c:\IETest
Imported my C:\test.htm file into the default app.rc resource file. I have changed the ID to be TEST
Compiled the DLL in debug mode
Altered the registry entry to read
res://C:\IETest\debug\IETest.dll/TEST
If I now restart IE and try again, the message box does not appear when I right-click and select my new context menu entry. I have also tried a release build of the DLL without any luck, and also tried replacing the last forward slash with a comma and altering the path single-backslashes to double-slashes.
I can only presume that I've done something wrong when creating my DLL. Can anyone point me in the right direction? Is there any way I can examine the compiled DLL to examine the resources and associated IDs?
Thanks.
Have you tried having the ID be TEST.html? My guess is that IE doesn't know how to handle the file because it doesn't have an extension listed, but this is totally a guess based off the fact that's how certain MS .dlls identify them (i.e. res://c:\windows\system32\shdoclc.dll/navcancl.htm)
The only other thing I can think of is to make sure your resources are of type 23.
ResourceHacker can view the resource files like you want: http://angusj.com/resourcehacker/