0

I am currently reading a pdf file an storing the data using fileManager, everything was working with my code, but now I need to add an authorization token to the headers of the request, but I am a bit confused on how and where I can add this token in the URL.


import Foundation
class FileManagementResponse{
    var url: URL
    var urlResponseSuccesfull: Bool
    
    init(url: URL, urlResponseSuccesfull: Bool) {
        self.urlResponseSuccesfull = urlResponseSuccesfull
        self.url = url
    }
}

class FileManagementUtil {
    
    static func downloadFromString(url: String, fileName: String) -> FileManagementResponse{
        let url = url
        return createPDFLocalURL(urlString: url, fileName: fileName)
    }
    
    static private func createPDFLocalURL(urlString:String, fileName:String) -> FileManagementResponse {
        //NEED TO ADD CREDENTIALS TO THIS REQUEST
        let url = URL(string: urlString)
        let pdfData = try? Data.init(contentsOf: url!)
        let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
        let pdfNameFromUrl = fileName
        let actualPath = resourceDocPath.appendingPathComponent(pdfNameFromUrl)
        do {
            try pdfData?.write(to: actualPath, options: .atomic)
            do {
                let resources = try actualPath.resourceValues(forKeys:[.fileSizeKey])
                let fileSize = resources.fileSize!
                print(fileSize)
                return FileManagementResponse(url: actualPath, urlResponseSuccesfull: true)
            } catch {
                return FileManagementResponse(url: actualPath, urlResponseSuccesfull: false)
            }
            
            //file is downloaded in app data container, I can find file from x code > devices > MyApp > download Container >This container has the file
        } catch {
            return FileManagementResponse(url: actualPath, urlResponseSuccesfull: false)
        }
    }
}

here is my code of the class I am using currently working with no AUTH to store pdf files

Ricardo Guerrero
  • 354
  • 1
  • 2
  • 15
  • Does this answer your question? [Is possible to add Headers to an URL swift](https://stackoverflow.com/questions/68932226/is-possible-to-add-headers-to-an-url-swift) – mahal tertin Aug 26 '21 at 07:25

0 Answers0