How to download and edit text-file with URL in SWIFT (Xcode 13)

Tommy Nguyen
3 min readSep 12, 2021

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.

Class Download for URL link in general

We can download the file simply using the command with certain URL:

Replace URL with another link, I just take my github link as an example.

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

Replace the time variable depend on the size of target file.

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:

--

--

Tommy Nguyen
0 Followers

I am a deep Learning Researcher, who love using statistics, Machine Learning/Deep Learning models to help us all lead happier lives.