My app should donate a shortcut to the shortcuts app, to retrieve a random image. However I am unable to return an image, and do understand how it should be done.
My intents file is as following. As "image" aint a possible return type, I instead return a file.
My code:
class TestIntentHandler: NSObject, TestIntentIntentHandling {
func handle(intent: TestIntentIntent, completion: @escaping (TestIntentIntentResponse) -> Void) {
let response = TestIntentIntentResponse(code: .success, userActivity: nil)
let data = UIImage(named: "test")!.pngData()!
response.image = INFile(data: data, filename: "tester", typeIdentifier: "png")
completion(response)
}
func confirm(intent: TestIntentIntent, completion: @escaping (TestIntentIntentResponse) -> Void) {
completion(TestIntentIntentResponse(code: .ready, userActivity: nil))
}
}
But testing the shortcut dont show the image.
Tried to run the same kind of code, and found that Shortcut recognizes the file as image if you give it the right typeIdentifier
(UTI) while creating the INFile
object. For example, to show a PNG file, you would use "public.png"
Here's an example of Shortcut showing a action result as an image
Now your question could be "Where do I get those identifiers from?". Well, that depends on your target iOS version. You could use UTTypeCreatePreferredIdentifierForTag(iOS <14) or UTType (iOS 14+), extract it directly from the URL if it contains it, or you can read the Apple reference list here
i'll leave here the UTTypeCreatePreferredIdentifierForTag example, courtesy of this answer
let fileExtension = "png" // This could be extracted from an URL
let uti = UTTypeCreatePreferredIdentifierForTag(
kUTTagClassFilenameExtension,
fileExtension,
nil)
... and the URL example, courtesy of this other answer
extension URL {
var typeIdentifier: String? { (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier }
var localizedName: String? { (try? resourceValues(forKeys: [.localizedNameKey]))?.localizedName }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With