How to download and edit text-file with URL in SWIFT (Xcode 13)
Xcode 13 has release verison 13 beta 5 for dev to check and play around with few changes. Some categories and layout has been grouped for better UI. Comparing previous Xcode version, Xcode 13 beta 5 have been updated its security for keep data safely. It leads to few methods can’t perform well due to new updates. In this paper, let’s find a way to download text file from URL and simple edit in code.
Normally, NSURLSessionConfiguration
is used for downloading file from URL but for Swift 4 and Swift 5, this way seems have some issues. The best way to get it by using simply URLSessionConfiguration
.
I would like to show few basic steps to explain how download the text file from URL. Firstly, creating a class for usages and error handling. The class will need to have check and validate the URL for certain file and avoid duplicating download file. You don’t need to change your filename, this class will name following the original file.
We can download the file simply using the command with certain URL:
To access the download file, you must delay the time to access. This is because Xcode need to run the code after the file is already downloaded. The best way to delay the is DispatchQueue
The basic way to load the file is find the your URL link and load data from it with String format. You can also convert the string to array by the separating with certain Regular expression like "," or "/n".
That is a simple way to downloading textfile and access the data with a few lines of codes. I have searched few other way but it’s doesn’t work well with Xcode 13. I find it very time-saving from those codes for my first app buiding. Hopefully you guys can find some useful from my post.
References: