Posts

Post not yet marked as solved
2 Replies
2k Views
My problem I have a SwiftUI app that uses PKCanvasView to allow drawing. The PKCanvasView is transparent and there are SwiftUI views (e.g. an Image) behind which can be dragged and opened. I want the user to be able to draw on the PKCanvasView with their apple pencil and interact with the views below with their finger... I can't figure out how to enable simultaneous interaction with the PKCanvasView (i.e. if using apple pencil draw) and the swiftUI views behind (i.e. if using finger, drag and move views). The context Here is my PKCanvasView wrapped in a UIViewRepresentable struct: struct CanvasView: UIViewRepresentable { @Binding var canvasView: PKCanvasView @State var toolPicker = PKToolPicker() func makeUIView(context: Context) - PKCanvasView { canvasView.drawingPolicy = .pencilOnly canvasView.backgroundColor = .clear canvasView.isOpaque = false canvasView.alwaysBounceVertical = true canvasView.alwaysBounceHorizontal = true toolPicker.setVisible(true, forFirstResponder: canvasView) toolPicker.addObserver(canvasView) return canvasView } func updateUIView(_ uiView: PKCanvasView, context: Context) { } } The structure of my content view is as follows: swift ZStack(alignment: .topLeading){                 CanvasView(canvasView: $canvasView)                     .zIndex(0)                 ForEach(canvas.subCanvases.indices, id: \.self){ index in                         CanvasCardView(                             canvas: $canvas.subCanvases[index],                             animationNamespace: animationNamespace,                             onReorder: {                                 reorderCard(at: canvas.subCanvases[index].zOrder)                         })                 }             } The CanvasCardView are the background SwiftUI views which can be dragged and opened. The view attaches a negative zIndex to them which orders them behind the CanvasView (PKCanvasView). My attempts I've tried two approaches so far which don't work: Subclassing PKCanvasView to override point(inside:with:). Passing false when I want to touches to be passed through. Problem with this is that the swiftUI views never recognise the touches through the UIView. I assume as there are no UITouchs behind the scenes. Adding a @State property to my contentView that can update an allowsHitTesting() modifier dynamically when required. The issue with this is that without allowing the PKCanvasView to be "hit" I can't determine whether it is a pencil touch or not. So i can't properly interact with PKCanvasView simultaneously. Any other ideas if this is possible? That is, passing touches through a UIViewRepresentable dynamically? I really want to avoid shoving the SwiftUI views into a UIView (with UIHostingController) and then turning them back into SwiftUI. Many thanks!
Posted Last updated
.
Post not yet marked as solved
1 Replies
1.2k Views
I have a DataManager class as follows: enum DataManagerType { case normal, preview, testing } class DataManager: NSObject, ObservableObject { static let shared = DataManager(type: .normal) static let preview = DataManager(type: .preview) static let testing = DataManager(type: .testing) @Published var todos = [Todo]() fileprivate var managedObjectContext: NSManagedObjectContext private let todosFRC: NSFetchedResultsController<TodoMO> private init(type: DataManagerType) { switch type { case .normal: let persistentStore = PersistentStore() self.managedObjectContext = persistentStore.context case .preview: let persistentStore = PersistentStore(inMemory: true) self.managedObjectContext = persistentStore.context for i in 0..<10 { let newTodo = TodoMO(context: managedObjectContext) newTodo.title = "Todo \(i)" newTodo.isComplete = false newTodo.date = Date() newTodo.id = UUID() } try? self.managedObjectContext.save() case .testing: let persistentStore = PersistentStore(inMemory: true) self.managedObjectContext = persistentStore.context } let todoFR: NSFetchRequest<TodoMO> = TodoMO.fetchRequest() todoFR.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)] todosFRC = NSFetchedResultsController(fetchRequest: todoFR, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil) super.init() // Initial fetch to populate todos array todosFRC.delegate = self try? todosFRC.performFetch() if let newTodos = todosFRC.fetchedObjects { self.todos = newTodos.map({todo(from: $0)}) } } func saveData() { if managedObjectContext.hasChanges { do { try managedObjectContext.save() } catch let error as NSError { NSLog("Unresolved error saving context: \(error), \(error.userInfo)") } } } private func fetchFirst<T: NSManagedObject>(_ objectType: T.Type, predicate: NSPredicate?) -> Result<T?, Error> { let request = objectType.fetchRequest() request.predicate = predicate request.fetchLimit = 1 do { let result = try managedObjectContext.fetch(request) as? [T] return .success(result?.first) } catch { return .failure(error) } } } My persistence store is as such: struct PersistentStore { let container: NSPersistentContainer init(inMemory: Bool = false) { container = NSPersistentContainer(name: "CoreDataModel") if inMemory { container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") } container.viewContext.automaticallyMergesChangesFromParent = true container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) } var context: NSManagedObjectContext { container.viewContext } func saveContext () { if context.hasChanges { do { try context.save() } catch let error as NSError { NSLog("Unresolved error saving context: \(error), \(error.userInfo)") } } } } I get an error when calling: let predicate = NSPredicate(format: "id = %@", todo.id as CVarArg) //todo.id is just some UUID() //irrelevant here let result = fetchFirst(TodoMO.self, predicate: predicate) This is the error: 2022-07-09 21:36:17.425709-0400 CoreDataExample[73965:7495035] [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'CoreDataExampleTests.TodoMO' so +entity is confused. Have you loaded your NSManagedObjectModel yet ? CoreData: error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'CoreDataExampleTests.TodoMO' so +entity is confused. Have you loaded your NSManagedObjectModel yet ? 2022-07-09 21:36:17.425780-0400 CoreDataExample[73965:7495035] [error] error: +[CoreDataExampleTests.TodoMO entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass CoreData: error: +[CoreDataExampleTests.TodoMO entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass /Users/santiagogarciasantos/Documents/Xcode Projects/CoreDataExample/CoreDataExample/DataManager/DataManager.swift:87: error: -[CoreDataExampleTests.CoreDataExampleTests test_Add_Todo] : executeFetchRequest:error: A fetch request must have an entity. (NSInvalidArgumentException) I've checked the target, made sure my Entity is in "Current Product Module" but it still won't work. Important: This only occurs when I'm testing (using my DataManager.testing)
Posted Last updated
.
Post not yet marked as solved
3 Replies
2.6k Views
I would like to change the background color of a SwiftUI text editor on macOS. Is there a variant for the code below (used for iOS) to work for NSTextField instead of UITextView? Thanks. struct ContentView: View { @State var text = "" &#9;&#9;init() { &#9;&#9;&#9;&#9;UITextView.appearance().backgroundColor = .clear &#9;&#9;} &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;TextEditor(text: text) &#9;&#9;&#9;&#9;&#9;&#9;.background(Color.red) &#9;&#9;} }
Posted Last updated
.
Post not yet marked as solved
1 Replies
595 Views
I have a note-taking app that enables persistence of data using CoreData. I would like to be able to attach a document to a note and it to be stored persistently, so that when the user taps the attachment they are provided with an action sheet to open in another app, share, copy etc... Additionally, I would like this to work cross device using CloudKit so I can't just store a bookmark or url using bookmarkData(options:includingResourceValuesForKeys:relativeTo:). I have figured that using UIDocument would be the best way to handle the file but I'm not sure how to save this as an attribute to a core data entity or even if this is the best way to go about this. It seems like a common premise: saving a file attachment. Many thanks.
Posted Last updated
.
Post not yet marked as solved
0 Replies
472 Views
I simply want to know if the following process would work: Create SwiftUI app for iOS and MacOS using Catalyst. Publish to App Store At a later date, migrate MacOS app to a native multiplatform app.
Posted Last updated
.
Post marked as solved
34 Replies
16k Views
Is it possible to use CoreData with the newly announces SwiftUI App Protocol for 100% SwiftUI apps. If I need to create an app with persistant storage, is there a way to achieve this with the new protocol? I like the idea of having my app fully compatable across all systems. Thanks.
Posted Last updated
.