0
ファイルをダウンロードして添付しようとしているUNNotificationServiceExtension
を書いています。私が走るたびに、例外が発せられます。UNNotificationServiceExtensionの添付ファイルのURLが無効です
添付ファイルのURLが無効です。
なぜ私はこれが失敗しているのか理解できません。
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
let bestAttemptContent = request.content.mutableCopy() as! UNMutableNotificationContent
guard let urlPath = request.content.userInfo["media-url"] as? String,
let url = URL(string: urlPath) else {
contentHandler(bestAttemptContent)
return
}
let semaphore = DispatchSemaphore(value: 0)
URLSession.shared.downloadTask(with: url) {
location, _, _ in
defer { semaphore.signal() }
guard let location = location else { return }
var destination: URL!
do {
destination = try FileManager.default.url(for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: location,
create: true)
destination.appendPathComponent(url.lastPathComponent)
let attachment = try UNNotificationAttachment(identifier: "",
url: destination)
bestAttemptContent.attachments = [attachment]
} catch let error {
bestAttemptContent.body = error.localizedDescription + "\n" + destination.absoluteString
}
}.resume()
_ = semaphore.wait(timeout: .distantFuture)
contentHandler(bestAttemptContent)
}