SwiftUI #State not updated in older project - xcode

I'm trying to add a simple SwiftUI view to an existing project, but #State changes are not propagated properly. The code works as expected in a new project, but doesn't work in the older project. The same thing happens with #ObservedObject.
Code:
struct ChannelAccountsView: View {
#State private var showGreeting = true
var body: some View {
VStack {
Toggle(isOn: $showGreeting) {
Text("Show welcome message")
}.padding()
if showGreeting {
Text("Hello World!")
}
}
}
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
let contentView = ChannelAccountsView()
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
...
}
New project:
Old project:
Tried all of the obvious fixes:
Edited AppDelegate and added SceneDelegate
Edited Info.plist
Cleaned Project and the Derived data
Reset simulator data
Tested on multiple simulators/devices
Any idea what might be going on?

I had a similar issue and the root cause was that my target had the Reflection Metadata Level set to None under Swift Compiler - General category inside Build Settings.
Changing it to All fixed it for me:

Creating a new Target within the Project resolved the issues. Still not sure what the root cause is.

Related

Rendering bug? when using transparent NSWindow with SwiftUI for MacOS app

I was developing a macOS app using SwiftUI (latest version from XCode 14.1) with the following App setup
import SwiftUI
#main
struct DemoApp: App {
#NSApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
var body: some Scene {
Window("demo", id: "demo") {
ContentView()
}.windowStyle(.hiddenTitleBar)
}
}
class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
#Published var active: Bool = true
var mainWindow: NSWindow? {
for w in NSApplication.shared.windows {
if w.title == "demo" {
return w
}
}
return nil
}
private func setupMainWindow() {
}
#MainActor func applicationDidFinishLaunching(_: Notification) {
guard let w = mainWindow else {
print("nothing to setup")
return
}
w.level = .floating
w.backgroundColor = NSColor.clear
w.isOpaque = false
w.standardWindowButton(.zoomButton)?.isHidden = true
w.standardWindowButton(.closeButton)?.isHidden = true
w.standardWindowButton(.miniaturizeButton)?.isHidden = true
}
}
The content view is defined as following
import SwiftUI
struct ContentView: View {
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
#State var counter = 0
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world! \(counter)").font(.system(size: 20).monospaced())
}
.padding().onReceive(timer) { _ in
counter += 1
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Basically what this app does is render a transparent background window with some content on it. The content has a static image and a text that updates every second (with a counter).
The initial rendering of the window looks ok, but once the content starts update, there are two duplicated artifacts being rendered: one from the first rendering result and second from the latest rendering result. The second is overlapped on top the first one.
Here is a screenshot of the window rendering on top of a dark colored background (another window):
Here is a screenshot of the window rendering on top of a white colored background (another window):
It seems to me when the window initially launches, because it's transparent, somehow the OS or framework is adding some outline to the rendering content, as you can see from the "Hello world 0" string, there is a outline there. Maybe the framework is trying to do something to differentiate the window content from other content below it?
When the window starts updating the content, the new content will be re-drawn, but the initial captured outline is still there, leaving this weird artifact that doesn't match the current rendered content.
I've tried to disable the transparent window (with solid background or background with non-zero alpha). That will completely eliminate the issue. The added outline is only seen when the window is completely transparent. As far as I can tell, it's related to the transparent window background and the outline added to the content.
Update: using hasShadow=false won't remove the border completely (the top is still visible on black background).
I'm configuring the window in a slightly different way, without the need for an AppDelegate, but I'm seeing the same thing. With a backgroundColor of .clear, not only is the rendering artefact apparent, also the window has no border.
Using a NSColor.white.withAlphaComponent(0.00001) as the backgroundColor eliminates the rendering issue, and also shows the border correctly.
struct Mac_SwiftUI_testApp: App {
var body: some Scene {
Window("demo", id: "demo") {
ContentView()
.task {
NSApplication.shared.windows.forEach { window in
guard window.identifier?.rawValue == "demo" else { return }
window.standardWindowButton(.zoomButton)?.isHidden = true
window.standardWindowButton(.closeButton)?.isHidden = true
window.standardWindowButton(.miniaturizeButton)?.isHidden = true
window.backgroundColor = NSColor.white.withAlphaComponent(0.00001)
window.isOpaque = false
window.level = .floating
}
}
}
.windowStyle(.hiddenTitleBar)
}
}
With backgroundColor = .clear
With backgroundColor = NSColor.white.withAlphaComponent(0.00001)
After trying a few things out, I found that setting
window.hasShadow = false
will fix your problem. It also removes the top "line" on the clear window

How to observe for modifier key pressed (e.g. option, shift) with NSNotification in SwiftUI macOS project?

I want to have a Bool property, that represents that option key is pressed #Publised var isOptionPressed = false. I would use it for changing SwiftUI View.
For that, I think, that I should use Combine to observe for key pressure.
I tried to find an NSNotification for that event, but it seems to me that there are no any NSNotification, that could be useful to me.
Since you are working through SwiftUI, I would recommend taking things just a step beyond watching a Publisher and put the state of the modifier flags in the SwiftUI Environment. It is my opinion that it will fit in nicely with SwiftUI's declarative syntax.
I had another implementation of this, but took the solution you found and adapted it.
import Cocoa
import SwiftUI
import Combine
struct KeyModifierFlags: EnvironmentKey {
static let defaultValue = NSEvent.ModifierFlags([])
}
extension EnvironmentValues {
var keyModifierFlags: NSEvent.ModifierFlags {
get { self[KeyModifierFlags.self] }
set { self[KeyModifierFlags.self] = newValue }
}
}
struct ModifierFlagEnvironment<Content>: View where Content:View {
#StateObject var flagState = ModifierFlags()
let content: Content;
init(#ViewBuilder content: () -> Content) {
self.content = content();
}
var body: some View {
content
.environment(\.keyModifierFlags, flagState.modifierFlags)
}
}
final class ModifierFlags: ObservableObject {
#Published var modifierFlags = NSEvent.ModifierFlags([])
init() {
NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) { [weak self] event in
self?.modifierFlags = event.modifierFlags
return event;
}
}
}
Note that my event closure is returning the event passed in. If you return nil you will prevent the event from going farther and someone else in the system may want to see it.
The struct KeyModifierFlags sets up a new item to be added to the view Environment. The extension to EnvironmentValues lets us store and
retrieve the current flags from the environment.
Finally there is the ModifierFlagEnvironment view. It has no content of its own - that is passed to the initializer in an #ViewBuilder function. What it does do is provide the StateObject that contains the state monitor, and it passes it's current value for the modifier flags into the Environment of the content.
To use the ModifierFlagEnvironment you wrap a top-level view in your hierarchy with it. In a simple Cocoa app built from the default Xcode template, I changed the application SwiftUI content to be:
struct KeyWatcherApp: App {
var body: some Scene {
WindowGroup {
ModifierFlagEnvironment {
ContentView()
}
}
}
}
So all of the views in the application could watch the flags.
Then to make use of it you could do:
struct ContentView: View {
#Environment(\.keyModifierFlags) var modifierFlags: NSEvent.ModifierFlags
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
if(modifierFlags.contains(.option)) {
Text("Option is pressed")
} else {
Text("Option is up")
}
}
.padding()
}
}
Here the content view watches the environment for the flags and the view makes decisions on what to show using the current modifiers.
Ok, I found easy solution for my problem:
class KeyPressedController: ObservableObject {
#Published var isOptionPressed = false
init() {
NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) { [weak self] event -> NSEvent? in
if event.modifierFlags.contains(.option) {
self?.isOptionPressed = true
} else {
self?.isOptionPressed = false
}
return nil
}
}
}

SwiftUI WindowGroup disable window persistence

Apple added new functionality to SwiftUI this year, bringing persistence and multiple windows to our SwiftUI apps. How can we disable window persistence. I'm looking for a windowing system very similar to Xcode, where there's a Welcome window on start, users can open new windows with the content they're looking for, then on the next start of the app only the Welcome window is shown.
The below code achieves all of these goals except the unwanted windows remain
import SwiftUI
#main
struct StackApp: App {
#Environment(\.openWindow) var openWindow
var body: some Scene {
Window("Welcome to App", id: "welcome-to-app") {
VStack {
Text("Welcome")
Button(action: {
openWindow(id: "app-content")
}) {
Text("Open Content")
}
}
}
.defaultSize(CGSize(width: 200, height: 200))
WindowGroup(id: "app-content") {
VStack {
Text("App Content")
}
}
.defaultSize(CGSize(width: 200, height: 200))
}
}
Help is much appreciated
Here's a quick hack proof-of-concept workaround. Can definitely be cleaned up, but it seems to work in macOS 12.6.1.
Not pretty but if you adopt the SwiftUI app lifecycle there just aren't as many ways too hook in and override the system default behavior (can't override the default NSDocumentController etc).
import SwiftUI
#main
struct TestWindowPersistenceApp: App {
#NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
DocumentGroup(newDocument: TestWindowPersistenceDocument()) { file in
ContentView(document: file.$document)
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillFinishLaunching(_ notification: Notification) {
print("did finish launching")
flushSavedWindowState()
// trigger open new file or Welcome flow here
}
func flushSavedWindowState() {
do {
let libURL = try FileManager.default.url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
guard let appPersistentStateDirName = Bundle.main.bundleIdentifier?.appending(".savedState") else { print("get bundleID failed"); return }
let windowsPlistFilePath = libURL.appendingPathComponent("Saved Application State", isDirectory: true)
.appendingPathComponent(appPersistentStateDirName, isDirectory: true)
.appendingPathComponent("windows.plist", isDirectory: false)
.path
print("path to remove: ", windowsPlistFilePath)
try FileManager.default.removeItem(atPath: windowsPlistFilePath)
} catch {
print("exception: \(error)")
}
}
}
Check out my Swift package which should solve this problem.
You could use it like this:
#main
struct MyApp: App {
var body: some Scene {
WindowGroup(id: "MyWindow") {
ContentView()
}
.register("MyWindow")
.disableRestoreOnLaunch()
}
}
It seems to work fine and achieved this by setting the isRestorable property of NSWindow to false. This should disable the default behavior.

Create a share sheet in iOS 15 with swiftUI

I am trying to create a share sheet to share a Text, it was working fine in iOS 14 but in iOS 15 it tells me that
'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a
relevant window scene instead.
how can I make it work on iOS 15 with SwiftUI
Button {
let TextoCompartido = "Hola 😀 "
let AV = UIActivityViewController(activityItems: [TextoCompartido], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(AV, animated: true, completion: nil)
}
I think you would be best served using SwiftUI APIs directly. Generally, I would follow these steps.
Create SwiftUI View named ActivityView that adheres to UIViewControllerRepresentable. This will allow you to bring UIActivityViewController to SwiftUI.
Create an Identifiable struct to contain the text you'd like to display in the ActivityView. Making this type will allow you to use the SwiftUI sheet API and leverage SwiftUI state to tell the app when a new ActivityView to be shown.
Create an optional #State variable that will hold on to your Identifiable text construct. When this variable changes, the sheet API will perform the callback.
When the button is tapped, update the state of the variable set in step 3.
Use the sheet API to create an ActivityView which will be presented to your user.
The code below should help get you started.
import UIKit
import SwiftUI
// 1. Activity View
struct ActivityView: UIViewControllerRepresentable {
let text: String
func makeUIViewController(context: UIViewControllerRepresentableContext<ActivityView>) -> UIActivityViewController {
return UIActivityViewController(activityItems: [text], applicationActivities: nil)
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ActivityView>) {}
}
// 2. Share Text
struct ShareText: Identifiable {
let id = UUID()
let text: String
}
struct ContentView: View {
// 3. Share Text State
#State var shareText: ShareText?
var body: some View {
VStack {
Button("Show Activity View") {
// 4. New Identifiable Share Text
shareText = ShareText(text: "Hola 😀")
}
.padding()
}
// 5. Sheet to display Share Text
.sheet(item: $shareText) { shareText in
ActivityView(text: shareText.text)
}
}
}
For the future, iOS 16 will have the ShareLink view which works like this:
Gallery(...)
.toolbar {
ShareLink(item: image, preview: SharePreview("Birthday Effects"))
}
Source: https://developer.apple.com/videos/play/wwdc2022/10052/
Time code offset: 25 minutes 28 seconds
To avoid warning, change the way you retrieve the window scene.
Do the following:
Button {
let TextoCompartido = "Hola 😀 "
let AV = UIActivityViewController(activityItems: [TextoCompartido], applicationActivities: nil)
let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
windowScene?.keyWindow?.rootViewController?.present(AV, animated: true, completion: nil)
}
Tested in in iOS 15 with SwiftUI
func shareViaActionSheet() {
if vedioData.vedioURL != nil {
let activityVC = UIActivityViewController(activityItems: [vedioData.vedioURL as Any], applicationActivities: nil)
UIApplication.shared.currentUIWindow()?.rootViewController?.present(activityVC, animated: true, completion: nil)
}
}
To avoid iOS 15 method deprecation warning use this extension
public extension UIApplication {
func currentUIWindow() -> UIWindow? {
let connectedScenes = UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.compactMap { $0 as? UIWindowScene }
let window = connectedScenes.first?
.windows
.first { $0.isKeyWindow }
return window
}
}
you could try the following using the answer from: How to get rid of message " 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead" with AdMob banner?
Note that your code works for me, but the compiler give the deprecation warning.
public extension UIApplication {
func currentUIWindow() -> UIWindow? {
let connectedScenes = UIApplication.shared.connectedScenes
.filter({
$0.activationState == .foregroundActive})
.compactMap({$0 as? UIWindowScene})
let window = connectedScenes.first?
.windows
.first { $0.isKeyWindow }
return window
}
}
struct ContentView: View {
let TextoCompartido = "Hola 😀 "
var body: some View {
Button(action: {
let AV = UIActivityViewController(activityItems: [TextoCompartido], applicationActivities: nil)
UIApplication.shared.currentUIWindow()?.rootViewController?.present(AV, animated: true, completion: nil)
// This works for me, but the compiler give the deprecation warning
// UIApplication.shared.windows.first?.rootViewController?.present(AV, animated: true, completion: nil)
}) {
Text("Hola click me")
}
}
}

Streamlining SwiftUI Previews in Xcode

There's a lot of code my app normally runs that I would like to skip in Previews. Code that is time-consuming and has no visible effects (such as initializing audio devices). I'm trying to figure out how skip it for previews.
There is an easy way to run code only in the production build of an app using the DEBUG macro. But I don't know of anything similar for non-Preview builds (because Previews presumably build the same code as non-Previews).
I thought that setting a variable, previewMode, within my ViewModel, would work. That way I could set it to true only within the PreviewProvider:
struct MainView_Previews: PreviewProvider {
static var previews: some View {
let vm = ViewModel(previewMode: true)
return MainView(viewModel: vm)
}
}
and when I created the ViewModel within the SceneDelegate, I could set previewMode to false:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let vm = ViewModel(previewMode: false)
let mainView = MainView(viewModel: vm)
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: mainView)
self.window = window
window.makeKeyAndVisible()
}
}
so that I can enclose any code I don't want to run for previews in if !previewMode { ••• }
Unfortunately the code is still running. Evidently the scene() function is getting called whenever my preview updates. :(
How can I specify code to not run for previews?
thanks!
The only working solution I've found is to use the ProcessInfo.processInfo.environment value for key XCODE_RUNNING_FOR_PREVIEWS. It's set to "1" only when running in preview mode:
let previewMode: Bool = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
See this post.
Practically Live-Preview mode run-time does not differ much from Simulator Debug mode run-time. And this, of course, as intended to give us quick (as possible) feedback of our code execution.
Anyway here are some findings... that might be used as solution/workaround for some cases that detection of Preview is highly desirable.
So created from scratch SwiftUI Xcode template project and in all functions of generated entities add print(#function) instruction.
ContentView.swift
import SwiftUI
struct ContentView: View {
init() {
print(#function)
}
var body: some View {
print(#function)
return someView()
.onAppear {
print(#function)
}
}
private func someView() -> some View {
print(#function)
return Text("Hello, World!")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
print(#function)
return ContentView()
}
}
Perform Debug Preview and see output:
application(_:didFinishLaunchingWithOptions:)
application(_:configurationForConnecting:options:)
scene(_:willConnectTo:options:)
init()
sceneWillEnterForeground(_:)
sceneDidBecomeActive(_:)
2020-06-12 16:08:14.460096+0300 TestPreview[70945:1547508] [Agent] Received remote injection
2020-06-12 16:08:14.460513+0300 TestPreview[70945:1547508] [Agent] Create remote injection Mach transport: 6000026c1500
2020-06-12 16:08:14.460945+0300 TestPreview[70945:1547482] [Agent] No global connection handler, using shared user agent
2020-06-12 16:08:14.461216+0300 TestPreview[70945:1547482] [Agent] Received connection, creating agent
2020-06-12 16:08:15.355019+0300 TestPreview[70945:1547482] [Agent] Received message: < DTXMessage 0x6000029c94a0 : i2.0e c0 object:(__NSDictionaryI*) {
"updates" : <NSArray 0x7fff8062cc40 | 0 objects>
"id" : [0]
"scaleFactorHint" : [3]
"providerName" : "11TestPreview20ContentView_PreviewsV"
"products" : <NSArray 0x600000fcc650 | 1 objects>
} > {
"serviceCommand" : "forwardMessage"
"type" : "display"
}
__preview__previews
init()
__preview__body
__preview__someView()
__preview__body
__preview__body
__preview__someView()
__preview__body
As it is clear complete workflow of app launching has been performed at start AppDelegate > SceneDelegate > ContentView > Window and only after this the PreviewProvider part.
And in this latter part we see something interesting - all functions of ContentView in Preview mode have __preview prefix (except init)!!
So, finally, here is possible workaround (DISCLAIMER!!! - on your own risk - only demo)
The following variant
struct ContentView: View {
var body: some View {
return someView()
.onAppear {
if #function.hasPrefix("__preview") {
print("Hello Preview!")
} else {
print("Hello World!")
}
}
}
private func someView() -> some View {
if #function.hasPrefix("__preview") {
return Text("Hello Preview!")
} else {
return Text("Hello World!")
}
}
}
Gives this

Resources