ios swift Dynamically update SSL Certificate(.cer) files

758 Views Asked by At

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?.

1

There are 1 best solutions below

1
On

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)