private func loadMediaDataAndCreateNotificationAttachment(with url:URL, defautType:String, completion:@escaping((UNNotificationAttachment?) ->Void)) {
letsession =URLSession(configuration: .default)
letdownloadTask = session.downloadTask(with: url) { [weakself] (url, response, error)in
guard let self = self else { return }
ifleturl = url {
lettypeString =self.determineType(fileType: response?.mimeType??"") ?? defautType
letpathUrl =self.getSavePathUrl(typeString: typeString)
do{
tryFileManager.default.moveItem(at: url, to: pathUrl)
letattachment =tryUNNotificationAttachment(identifier: typeString, url: pathUrl, options:nil)
completion(attachment)
}catch{
print("Error create attachment:\(error.localizedDescription)")
completion(nil)
}
}elseifleterror = error {
print("Error downloading attachment:\(error.localizedDescription)")
completion(nil)
}
}
downloadTask.resume()
}
网友评论