I learned that I can't have a file inside the bundle if I want it to be able to be changed while the app is running. If I have a file stored outside, how could I let an HTML website access that JS file? I'm using WKWebView to run a local website. The JS would need to be accessible by the website before it loads, because it is a configuration file that is needed on load.
Post
Replies
Boosts
Views
Activity
On a real iOS device, I am unable to write data to overwrite a file in my bundle. I am getting an error You don't have permission to save the file "CU_config.js" in folder "2023".
Here is the code that tries to write the file.
func configure() {
let picker = UIDocumentPickerViewController(forOpeningContentTypes: [.javaScript], asCopy: true)
picker.delegate = self
present(picker, animated: true)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
controller.dismiss(animated: true, completion: nil)
urls.forEach { url in
let sourceURL:URL
if(isMatch){
sourceURL = Bundle.main.bundleURL.appendingPathComponent("www/2023/CU_config").appendingPathExtension("js")
}else{
sourceURL = Bundle.main.bundleURL.appendingPathComponent("www/2023/CU_Pit_config").appendingPathExtension("js")
}
let destURL = url
let incoming:Data?
do {
incoming = try Data(contentsOf: destURL)
}catch{
incoming = nil
print("Unable to find file")
let alert = UIAlertController(title: "Error", message: "Unable to find file", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
do {
sourceURL.startAccessingSecurityScopedResource()
try incoming!.write(to: sourceURL)
sourceURL.stopAccessingSecurityScopedResource()
}catch{
let alert = UIAlertController(title: "Error", message: "Unable to write file"+error.localizedDescription, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
print("Unable to write file")
}
webview.reload()
}
}
I tried giving it permission in the info.plist by using "Application supports iTunes file sharing" but that didn't work.
It works perfectly in simulation.