Download and store a file in iOS

Download and store a file in iOS
February 23, 2020 No Comments Development,iOS Development Pushpendra Kumar

I’ve been working on an App that requires me to download PDF files, store it, and to view it. I thought to write a tutorial and make a video and share with YouTube. So that, other person also can take the benefit of this. In this tutorial, I have cover Download and store a file in iOS. This task contains the following topics here.

  • DownloadTask
  • File Management
  • PDFView

Download and store a file in iOS

For achieving all these we have to write some code below. First we will write the code for downloading the file. which is given as.

Very good, Now write one more extension code for URLSessionDownloadDelegate, So that we can handle the file storage. 

Above all, Used for downloading the file and store the file into proper location. After I pressed download, the file downloaded persisted less than a second, and then it was killed by the system.

This behaviour behaves the same on both simulator and physical machine. For finding the path of downloaded file in Simulator.

Open  ~/Library/Developer/CoreSimulator/Devices into finder. And complete location looks like. 
Temp file –file:///Users/pushpendra/Librar……D/tmp/CFNetworkDownload_tijImV.tmp

Actual file – file:///Users/pushpendra/Library…..Caches/swift_tutorial.pdf

Now crate a PDFViewController for opening the PDF file.

Great, Now write some code for opening the PDF File.

Now we’ve successfully placed the downloaded PDF into the proper place for users to access. It’s time to view it with PDFView from PDFKit, which is a convenient framework provided by Apple since the release of iOS 11.

Even though many tutorials of PDFKit uses storyboard to create PDFView by assigning the property to a UIView, it does not support in Xibs. Thus here I’m going to instantiate it programmatically.

Very good, Now below is the complete code of ViewController. 

In Conclusion, You have achieved your goal. For more information please watch the related video on the given LINK.

In addition, There are few more tutorial, Which may help you for good and stable iOS development.

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

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