save file in iPhone

In a modern iPhone app, a smooth user experience is quite important. To achieve this, caching the internet resources to local is a good approach. Therefore, to make the app more smoothly when users open it, downloading images and videos and saving them locally will quite improve the app starting speed. In the last tutorial, I have demonstrated how to download files by Swift 5 and watch the download status such as progress. The downloading task is controlled by 3 buttons: a start button to start, a pause button to pause and resume, a stop button to stop. In this tutorial, I will show how to save the downloaded file in local and open it by default in the iPhone. The whole tutorial is developed under the latest Swift.

To help you easy find the full set of these tutorial, here is the full list in this sessions:

Where to save local file in iPhone App

The source code of this post will be based on tutorial 1. After downloading the file, I will save the file to the local folder. In Swift, I can use FileManager to get the default app home directory file path. The following code shows how to get the current app’s home directory.

let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

After I get the path, I will build a full file path where to save the file with the file name. So I append the url’s last component to the directory path url.

self.localPath = documentsUrl.appendingPathComponent(url!.lastPathComponent)

In the last part, I will write the downloaded data to the final destination. The downloaded data is saved in a temporary file once it is downloaded by URLSession. And I can get the temporary file path in URLSessionDownloadDelegate.

let imageData = try Data(contentsOf: tempFile)
try imageData.write(to: self.localPath)

Final Source Code for Saving in Swift

Now, let’s combine the above code pieces into a complete function. The parameter of the function is the temporary file path which will be passed from URLSessionDownloadDelegate.

func saveToLocal(tempFile:URL) {
    let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    self.localPath = documentsUrl.appendingPathComponent(url!.lastPathComponent)
    
    do {
        let imageData = try Data(contentsOf: tempFile)
        try imageData.write(to: self.localPath)
        
        DispatchQueue.main.async {
            self.openBtn.isEnabled = true
        }
    } catch {
        print("Save failed")
    }
}

Open ePub in iPhone by Swift

To test saving the downloaded file successfully, I add an additional function in this app. I put a button to open the final file with the default App on the iPhone. To make it simple to understand, I am using UIDocumentInteractionController to handle it. Now let’s see the final app.

Download Source Code at $3.99

Now you can get the full source code under $3.99 with all copyright. The payment is handling by PayPal. After you complete the purchase, you will get the download link of the source code immediately.

Previous Post

Leave a Reply

Your email address will not be published. Required fields are marked *