I want to attach an image from Assets.xcassets to a notification. I have been looking for a solution for about an hour now and it seems like the only way to do this:
func createLocalUrl(forImageNamed name: String) -> URL? {
let fileManager = FileManager.default
let cacheDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let url = cacheDirectory.appendingPathComponent("\(name).png")
guard fileManager.fileExists(atPath: url.path) else {
guard
let image = UIImage(named: name),
let data = UIImagePNGRepresentation(image)
else { return nil }
fileManager.createFile(atPath: url.path, contents: data, attributes: nil)
return url
}
return url
}
Source : Can I get a NSURL from an XCAssets bundle?
Which seems like overkill. However as far as I can tell that's the only way to get an URL you can use:
let imageURL = createLocalUrl(forImageNamed: "TestImage")
let attachment = try! UNNotificationAttachment(identifier: "image", url: imageURL!, options: [:])
content.attachments = [attachment]
Is there a better way to add images from Assets.xcassets to local notifications? (Yes I know I can store images outside Assets and use Bundle.main.etc)