I want to open a text file where all the Licence stuff is written down, I want to do it like this in a macOS app, but the app Bundle does not work for some reason:
import SwiftUI
@main
struct menuHelpApp: App {
func androidTools(tool: URL, arguments: [String], completionHandler: @escaping (Int32, Data) -> Void) throws {
let group = DispatchGroup()
let pipe = Pipe()
var standardOutData = Data()
group.enter()
let proc = Process()
proc.executableURL = tool
proc.arguments = arguments
proc.standardOutput = pipe.fileHandleForWriting
proc.terminationHandler = { _ in
proc.terminationHandler = nil
group.leave()
}
group.enter()
DispatchQueue.global().async {
let data = pipe.fileHandleForReading.readDataToEndOfFile()
pipe.fileHandleForReading.closeFile()
DispatchQueue.main.async {
standardOutData = data
group.leave()
}
}
group.notify(queue: .main) {
completionHandler(proc.terminationStatus, standardOutData)
}
try proc.run()
pipe.fileHandleForWriting.closeFile()
}
var body: some Scene {
WindowGroup {
ContentView()
}
.commands {
CommandGroup(replacing: .help) {
let licenceUrl = Bundle.main.url(forResource: "textFiles/Licence", withExtension: "txt")
Button(action: {
try! androidTools(tool: URL(fileURLWithPath: "/usr/bin/open", relativeTo: nil), arguments: ["-a", "TextEdit", licenceUrl!.absoluteString]) { (status, outputData) in
let output = String(data: outputData, encoding: .utf8) ?? ""
print(output)
}
}) {
Text("AppSignerGui License")
}
}
}
}
}
Post
Replies
Boosts
Views
Activity
My app has two buttons, the first is calling a function that return a string and does some other stuff.
The second button has a function that takes the return String from the function one. Because the first function does other stuff as well, how can I store the return String in a variable for button two, so I don't have to call the function?
Example below, I want to avoid usingDirString (strUpper: returnDirString())
func returnDirString () -> String {
let dir = "/some/return/string"
return dir
}
func usingDirString (strUpper: String) {
print(strUpper)
}
struct ContentView: View {
var body: some View {
Button(action: {
returnDirString()
}) {
Text("First")
}
.padding()
Button(action: {
usingDirString (strUpper: returnDirString())
}) {
Text("second")
}
.padding()
}
}
If you use the sample code for a swiftUi App for MacOS the .onDelete function profided is not working, because .onDelete is only working in IOS. How can I use the .deleteItems function for MacOS?
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
animation: .default)
private var items: FetchedResults<Item>
var body: some View {
List {
ForEach(items) { item in
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
}
.onDelete(perform: deleteItems)
}
.toolbar {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
private func addItem() {
withAnimation {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
}