Post

Replies

Boosts

Views

Activity

Widget showsWidgetContainerBackground fallback
To bring my widgets to iPad lock screen, i need to detect my widget that has a background to display different contents, watch the wwdc 23 video it tells we can use .showsWidgetContainerBackground environment to apply that goal, but the problem is this code wouldn't compile because .showsWidgetContainerBackground only available in iOS 17 @available(iOS 14.0, *) struct MyWidgetView: View { @Environment(\.showsWidgetContainerBackground) var showsBackground var body: some View { ... } } So, my solution is use a environment wrapper, like this: @available(iOS 17.0, *) struct EnvironmentWrapper: View { @Environment(\.showsWidgetContainerBackground) var showsWidgetContainerBackground var body: some View { VStack(alignment: .leading) { Text("Wrapper Has Background: \(showsWidgetContainerBackground ? "YES" : "NO")") } } } extension View { var showsWidgetContainerBackground: Bool { if #available(iOS 17.0, *) { return EnvironmentWrapper().showsWidgetContainerBackground } else { return true } } } But it didn't work, this is the example: @available(iOS 17.0, *) struct EnvironmentWrapper: View { @Environment(\.showsWidgetContainerBackground) var showsWidgetContainerBackground var body: some View { VStack(alignment: .leading) { Text("Wrapper has background: \(showsWidgetContainerBackground ? "YES" : "NO")") } } } @available(iOS 17.0, *) struct ExampleView: View { @Environment(\.showsWidgetContainerBackground) var showsBackground var body: some View { VStack(alignment: .leading) { Text("Read from self: \(showsBackground ? "YES" : "NO")") EnvironmentWrapper() Text("Read from wrapper: \(EnvironmentWrapper().showsWidgetContainerBackground ? "YES" : "NO")") } .font(.system(size: 12, weight: .medium)) } } As you can see, if i read it directly from self, it works, but if i read from outside, it always return true i also try to load a view first in the wrapper, but it didn't work either func getShowsBackground() -> Bool { let _ = body return showsWidgetContainerBackground }
8
2
2.4k
Jul ’23
Crash: _NSMetadataQueryResultArray objectAtIndex
Hello, sometimes if I use NSMetadataQuery to obervse my file changes on macOS, it crash for this reason, its odd and i have no clue for this problem becuse in my code I never get results using index, anyone help? thanks! Application Specific Information: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_NSMetadataQueryResultArray objectAtIndex:]: index (251625) out of bounds (251625)' terminating with uncaught exception of type NSException abort() called
14
0
1.3k
Dec ’21
Widget Extension for macOS app?
In the Big Sur Apple has remove Today Extension and replace it to Widgets, so Today Extension is no longer exist. Today I try to add a Widget Extension in my macOS app: File -> New -> Target -> macOS, but there is no Widget Extension there, and I tried to add an iOS Widget Extension but it does not embed in my macOS app so not working. What the hack? do I have to rebuild a whole new cross platform app to archive that gold or something what? does anybody is meeting some problem here or macOS just being abandon??
2
0
1.6k
Sep ’20