I am developing an app which should run on Ipad, Iphone and Mac. From the app the user shall be able to press a button and copy a result (pure text) into the clipboard so it can be pasted into other apps on the same device (command + V).
This works fairly well on my Ipad (or Iphone) where I use this (IOS) code:
import SwiftUI
import UniformTypeIdentifiers
.....
UIPasteboard.general.setValue(output,
forPasteboardType: UTType.plainText.identifier)
....
The problem is that when I run this on "My Mac (Designed for iPad) - it does not copy the text when run on Mac as compatible iPad app.
I take note that to access the clipboard on MacOS you would need another (MacOS) code like this.
import Cocoa
...
let pasteboard = NSPasteboard.general()
pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
pasteboard.setString(output, forType: NSPasteboardTypeString)
....
The problem is that Cocoa can only be imported into native MacOS app - and while I recognise this could be handled with a lot of pre-processor statements separating native IOS and native MacOS code - it would highly complicate my code - and I would almost need to write the app twice just switching all the UI's to NS's.
#if os(iOS)
UIPasteboard.general.setValue(output,
forPasteboardType: UTType.plainText.identifier)
#elseif os(macOS)
let pasteboard = NSPasteboard.general()
pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
pasteboard.setString(output, forType: NSPasteboardTypeString)
#endif
Even for the clipboard example it would start to complicate things. Then add to this that have have several references to other UI-classes which would be NS-classes on a MacOS.
Can this be handled in a smarter way now we actually can run SwiftUI and IOS apps on a Mac????
I know one way could be to copy my output into a text-field from which the user could "Command + C" - but it is really not what I want.
I hope there is something clean and smart for this - so we can assume that IOS apps can actually run on Mac with full compatibility.
Thanks in advance