Created
August 16, 2016 12:53
-
-
Save janodev/160736b47ee9d2a96dfeecb50dc2b97c to your computer and use it in GitHub Desktop.
Recover a notification attachment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import UserNotifications | |
import UserNotificationsUI | |
struct Attachments { | |
static let image = (identifier:"image", jsonElement:"imageURL") | |
} | |
class ContentViewController: UIViewController, UNNotificationContentExtension | |
{ | |
@IBOutlet var kittenImage: UIImageView! | |
func didReceive(_ notification: UNNotification) | |
{ | |
if let image = imageIn(notification: notification, withIdentifier: Attachments.image.identifier) { | |
self.kittenImage.image = image | |
} | |
} | |
private func imageIn(notification:UNNotification, withIdentifier: String) -> UIImage? | |
{ | |
var image: UIImage? | |
let findImage: (UNNotificationAttachment)->Bool = { attachment in attachment.identifier == Attachments.image.identifier } | |
if let attachment = notification.request.content.attachments.first(where: findImage) { | |
if attachment.url.startAccessingSecurityScopedResource() { | |
image = UIImage(contentsOfFile: attachment.url.path) | |
attachment.url.stopAccessingSecurityScopedResource() | |
} | |
} | |
return image | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment