Post not yet marked as solved
791
Views
I have a layout similar to Music or Podcasts, where I have a sidebar and a grid content area. When clicking on one of the items in the grid I want to open a full screen detail view just like Music does. My question is: How can I display a back button in the toolbar? I only figured out how to add buttons to the trailing side of the toolbar. Is this even possible in pure SwiftUI?
.toolbar {
Button(action: showSearch) {
Label("Add Game", systemImage: "plus")
}
}
Post not yet marked as solved
1.5k
Views
Hello,
During the talk there is a sample code using new property wrapper @FocusedBinding.
Can you expand more on this topic with small example how to use this new property wrapper, there has been clear examples of @AppStorage, @StateObject, @SceneStorage however no talk expands more on @FocusedBinding.
The documentation is not very clear neither.
Thank you.
Post not yet marked as solved
748
Views
I have an Onboarding Window/Scene I want to appear on the first launch of the app, or when a use clicks a menu command.
Using an AppStorage with a boolean value would be perfect, but when I try to add an if in my SceneBuilder I get the following error: Closure containing control flow statement cannot be used with function builder 'SceneBuilder'.
import SwiftUI
@main
struct MyApp: App {
@AppStorage("tutorialVisible") var tutorialVisible: Bool = true
var body: some Scene {
MainScene()
if tutorialVisible {
TutorialScene()
}
}
}
How can this be done?
Post marked as solved
437
Views
Hi everyone,
When I add WatchOS as a target to my multi platform SwiftUI project, it creates its own ContentView within the Watchkit Extension folder. It does not seem to be able to access any views within the "Shared" folder in the multi platform App template, leaving me with two Content Views in the project.
Does anyone know how to add a WatchOS target and have it use the shared files? Can this be done?
I was encouraged because the App Essentials WWDC20 video implies WatchOS can be used with this template as they have a screenshot example, but so far haven't had success in duplicating it.
Post not yet marked as solved
563
Views
Aside from Settings (on macOS) is there a way to have different windows (e.g. a main UI and then a separate UI)?
I see that there's id: available which suggests multiple WindowGroups and being able to reference different groups.
And if this is possible, how can I manage the multiple WindowGroups?
Post not yet marked as solved
855
Views
Is it possible to only allow a single window instance on macOS?
WindowGroup/DocumentGroup allow the user to create multiple instances of a window. I'd like to only allow one, for an Onboarding sequence.
I've checked the Scene documentation - https://developer.apple.com/documentation/swiftui/scene, and it appears the only types conforming to the Scene protocol are WindowGroup, DocumentGroup and Settings. How can I create a single Window in a SwiftUI App?
An example use case:
struct TutorialScene: Scene {
var body: some Scene {
	// I don't want to allow multiple windows of this Scene!
	WindowGroup {
		TutorialView()
	}	
}
Post marked as solved
277
Views
Has anyone had success getting the .onDeleteCommand() modifier to work with a List?
.Delete() seems to be working fine, and enables swipe-to-delete on any platform, and I figured the onDeleteCommand modifier was the key to enabling the Edit > Delete menu on macOS, but no matter where I put the modifier in my view the Edit > Delete command remains disabled.
Post not yet marked as solved
272
Views
If any view contains reference to @SceneStorage in document app using the DocumentGroup scene type, the application upon running fails with fatal error.
Is this intentional or a kind of bug?
Post marked as solved
239
Views
As a long-time Obj-C dev (since iPhone OS 2) I decided that SwiftUI was the nudge to use Swift! ¯\\_(ツ)_/¯
Thus, I am still trying to understand how a language can be so type-vague in code, and so type-pedantic in the compiler!!!
Pulling my hair out trying to get Swift/SwiftUI to instantiate a UIHostingController<>, for use with CoreData
class MyCoreDataHostingController : UIHostingController<MyCoreDataView> {
required init?(coder: NSCoder) {//Instantiate From Storyboard
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let contentView = MyCoreDataView().environment(\.managedObjectContext, context)
super.init(coder: coder, rootView: contentView)
//Cannot convert value of type 'some View' to expected argument type 'MyCoreDataView'
}
}
struct MyCoreDataView: View {
var body: some View {
Text("Core Data View")
}
}
How can I FORCE Swift to INFER the correct/appropriate type?
What I have tried so far.
5 let contentView = MyCoreDataView()
Compiles/runs, but does not include CoreData.
6 super.init(coder: coder, rootView: contentView as! MyCoreDataView)
Compiles, but crashes at instantiation.
Could not cast value of type 'SwiftUI.ModifiedContent<MyHostingController.MyCoreDataView, SwiftUI.EnvironmentKeyWritingModifier<_C.NSManagedObjectContext>>' (...)
to 'MyHostingController.MyCoreDataView' (...). ... or is this simply not possible? (yet)
Post not yet marked as solved
406
Views
Could you please make a
comparison with @ObservedObject ?
thanks
Post marked as solved
649
Views
WWDC2020 videos said SwiftUI on Xcode12 can create and manage different scenes in an App, but we do not find out any API could do that. We try the method of SwiftUI on Xcode11 to create new Window on Xcode 12 :
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: nil, options: nil, errorHandler: nil) })
But it does not work as we expect. The code create a window of the same scene. If we set a different userActivity, SwiftUI have no method to set SceneDelegate.swift. It only has @UIApplicationDelegateAdaptor to set AppDelegate.
So, I Wonder whether there is a convenient way to create a new Scene by new SwiftUI APIs.
The following code can only create Scenes with the same content view :
@main struct test: App {
let newWindowPublisher = NotificationCenter.default.publisher(for: Notification.Name("anotherScene"))
var body: some Scene {
WindowGroup {
ContentView()
.onReceive(newWindowPublish, perform: { info in
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: nil, options: nil, errorHandler: nil) })
}
}
}
Post marked as solved
259
Views
With the recent changes to allow for an app to be completely in SwiftUI, I am trying to re-create a simple core data and SwiftUI app.
How can a NSPresistantContainer be setup with the new app delegate adaptor and scene builder? I have attempted what has worked previously by creating a lazy var in the app delegate and then providing that as an adaptor on the app struct.
What is the correct way to setup the NSPresistantContainer so the NSManagedObjectModel is loaded?
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if isDataSet() == false {
setDefaultData()
}
return true
}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "breakapp")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
func setDefaultData() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
for (i, weekday) in Constants.CoreData.weekdays.enumerated() {
let user = NSManagedObject(entity: Weekday.entity(), insertInto: managedContext)
user.setValue("\(weekday)", forKey: Constants.CoreData.Attributes.label)
user.setValue(i, forKey: Constants.CoreData.Attributes.sortNumber)
}
try? managedContext.save()
}
func isDataSet() -> Bool {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return true }
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: Constants.CoreData.Entities.weekday)
let result = try? managedContext.fetch(fetchRequest)
return result?.count != 0
}
}
@main
struct TakeABreakApp: App {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
@Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct TakeABreakApp_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Post not yet marked as solved
731
Views
The following warnings presented themselves when the Document groups example code is built from the App essentials in SwiftUI session.
warning: The application supports opening files, but doesn't declare whether it supports opening them in place. You can add an LSSupportsOpeningDocumentsInPlace entry or an UISupportsDocumentBrowser entry to your Info.plist to declare support. (in target 'NewInSwiftUI' from project 'NewInSwiftUI') And another "purple" warning:
Multiline Type "com.example.ShapeEdit.shapes" was expected to be declared and exported in the Info.plist of NewInSwiftUI.app, but it was not found. After adding the "Supports opening documents in place" info.plist entry and an "Exported Type Identifier" to the project target, the warning goes away whether this boolean is set to Yes or No. What does this plist entry do (apart from the obvious)?
Interestingly, the warning does not return if the "Supports opening documents in place" and "Exported Type Identifier" info.plist entries are removed. Xcode seems to be hanging on to the bones of the "Exported Type Identifier" which can be seen when a commit is initiated with Xcode Source Control.
Post not yet marked as solved
192
Views
Sometimes when an new, unexpected error, hang or deadlock occurs during testing, one might want to stop the app in the debugger and poke around. To do this, in a AppKit app, one would ask for the singleton [NSApplication sharedApplication] by that symbol or the NSApp convenience, and then from there drill down into the app delegate, windows, documents, or whatever was interesting.
How do I do that in a SwiftUI app which is based on App? Apparently, there is a single MyApp:App struct. How can I get the debugger on it and get its properties?
Post marked as solved
1.3k
Views
In the old SwiftUI app initialization there was UIApplicationDelegate and UIWindowSceneDelegate.
I was handling a URL callback in the SceneDelegate using the method:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>)
However in the new app initialization there is no SceneDelegate, nor an appropriate property like there is UIApplicationDelegateAdaptor for UIApplicationDelegate
I tried using the method but it is not called.
func application( _ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:] ) -> Bool
So my question is, how do I handle a callback url in the new app setup?