|
Hi all, I'm working on something I thought would be simple, and it turns out to be taking way longer than I expected and spent hours looking for a similar issue that would resolve my issue, to no avail. I've followed a tutorial to import the content of a file that is part of the application. That part works fine, no issues there. Now I want to update this import to allow the user to select a file from the Files application and then reads its content, the same way it does with the file part of the application. I've tried several things, so my code might be a little messed up, and not optimal yet. This is the part of the code that works, when using the file part of my application - note the path using
Now how I modified the function, to attempt to make it work with a custom path from
This is the error:
Adn this is how I request the user to select the file to import, in another file/view:
The print of the
To me, it seems the file can return its path and it could be used to read the content of it. I'm lost as to what I'm not understanding to make it work by selecting the file. I'm using the iOS Simulator, not a real device for the testing, if that makes a difference. Thank you very much for any assistance you can provide. |
|
Hi @mezzomix23,
You app cannot save files directly to bundle otherwise Apple OS wouldn't be so secure if everyone could access files in their directories. As you can see from your error you are trying to access file via bundle which is in absolutely different directotry File%20Provider%20Storage/MeasuresHistory.csv. And as it does not exist in bundle the error is thrown. I haven't tried this workaround but just my considerations. Using .fileImporter to get hold of contents of the file if needed or otherwise try to save it in userDomain location. In the example below I was using it to display the data on the screen instead of saving. If you need to display it direcly maybe it is the good way to start.
Then save the imported file in DocumentDirectory as in this lesson https://www.hackingwithswift.com/example-code/strings/how-to-save-a-string-to-a-file-on-disk-with-writeto Then you can have access to "your" file via file manager, as you will have the file saved in previous step.
|
|
Hi @ygeras, Thank you very much for your answer, it really helped. Indeed, I do not need to save the file in my app once imported, just read its content, so that piece of code, with some adjustements for my needs, did the trick:
Have a great week-end! |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.