0

Dynamically need to update. I'm able to download the certificate from a URL and save it to the document directory.

func downloadFile(url: URL, completion: @escaping (String?, Error?) -> Void) {
    let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)
    if FileManager().fileExists(atPath: destinationUrl)
    {
        print("File already exists [\(destinationUrl)]")
        completion(destinationUrl, nil)
    }
    if let dataFromURL = NSData(contentsOf: url)
    {
        if dataFromURL.write(to: URL(string: destinationUrl)!, atomically: true)
        {
            print("file saved [\(destinationUrl)]")
            completion(destinationUrl, nil)
        }
        else
        {
            print("error saving file")
            let error = NSError(domain:"Error saving file", code:1001, userInfo:nil)
            completion(destinationUrl, error)
        }
    }
    else
    {
        let error = NSError(domain:"Error downloading file", code:1002, userInfo:nil)
        completion(destinationUrl, error)
    }
}

My question is after download the file need to save into App Bundle (Bundle.main.resourcePath) directory.
or save it to the document folder and move it to the App Bundle directory?.

Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41

1 Answers1

1

You can't move stuff to the Bundle directory. This is a read only directory. You have to read them from Documents and use code to create certificate pinning by reading the files manually (or using Alamofire)

Alistra
  • 5,177
  • 2
  • 30
  • 42
  • I'm using Alamofire only, but Alamofire automatically take certificate app bundle only (Bundle.main.af.certificates) Is Alamofire provide any property to change the certificate directory. – Ramprasath Selvam Dec 14 '21 at 13:53