'Rename the download pdf file using swift iOS

I am successfully downloading a PDF from api end point. Once pdf is downloaded, the title of pdf is : PDF document.pdf . How to change the title of PDF?

I tried to update metadata of PDF using PDFDocumentAttribute (see below), but it is not working.

var metadata = pdfDocument.documentAttributes!
metadata[PDFDocumentAttribute.subjectAttribute] = "subject attribute"
metadata[PDFDocumentAttribute. titleAttribute] = "title attribute"
pdfDocument.documentAttributes = metadata

Note: I am not using FileManager

How I am fetching PDF:-

let task = session.dataTask(with: urlRequest) { (data, _, error) in
            DispatchQueue.main.async {
                guard let unwrappedData = data, error == nil else {
                    completion(.failure(error ?? Constants.dummyError))
                    return
                }

                guard let pdfDocument = PDFDocument(data: unwrappedData) else {
                    completion(.failure(error ?? Constants.dummyError))
                    return
                }

                completion(.success(pdfDocument))
            }
        }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source