I’m working into a problem reliably detecting what’s on the pasteboard and pasting information from the pasteboard to make use of in my app.
First: detecting pasteboard contents reliably
I’ve an app that makes use of a picture as enter. When there may be a picture on the pasteboard, I need to give the consumer an choice to stick the picture. When there may be not a picture on the pasteboard, I don’t need to give the consumer the choice to stick, since no matter is on the pasteboard won’t be helpful within the app.
To detect whether or not there may be a picture accessible on the pasteboard, I first used this:
UIPasteboard.basic.hasImages
I’ve additionally tried this different method, explicitly checking for particular forms of pictures. Right here I am together with the broader context of code that I am utilizing to verify the pasteboard any time the app involves the foreground:
class PasteboardProvider: ObservableObject {
@Revealed var pasteboardHasImages: Bool = false
init() {
NotificationCenter.default.writer(for: UIApplication.willEnterForegroundNotification, object: nil)
.map { _ in
let supportedTypes: Set<String> = ["public.heic",
"public.jpeg"]
let pasteboardTypes = UIPasteboard.basic.varieties
// Returns false when there are frequent parts (pictures on pasteboard)
let pasteboardContainsNoImages = supportedTypes.isDisjoint(with: pasteboardTypes)
return !pasteboardContainsNoImages
}
.assign(to: &$pasteboardHasImages)
}
}
Each of those appear to work more often than not, telling me whether or not or not there may be a picture on the pasteboard.
Nevertheless, typically (I haven’t discovered the precise situations that result in it) UIPasteboard.basic.hasImages shall be true, suggesting there may be a picture on the pasteboard (and, in truth, there may be a picture on the pasteboard).
However then, when I attempt to paste it, I get nil
. On account of how I course of the info in my app, I get it as information, and this information is what’s nil:
guard let information = UIPasteboard.basic.information(forPasteboardType: "public.picture")
Am I lacking a step or one thing? What might trigger this paste to fail, even after I’ve simply checked the pasteboard, and know that it comprises a picture?