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?.
You can't move stuff to the
Bundledirectory. This is a read only directory. You have to read them fromDocumentsand use code to create certificate pinning by reading the files manually (or using Alamofire)