-2

I have an application which downloads file from the server. i want to open these files present in the file system in a new view controller.

What can I do to open the files without using web view

Amon
  • 99
  • 1
  • 9
  • What kind of files types you are using? – Gourav Joshi Dec 27 '16 at 10:54
  • Types of files are txt, pdf and may be plist. – Amon Dec 27 '16 at 10:58
  • For Text you need either `UITextView, UITextField, or UILabel`, for Image -. `UIImageView`, for PDF you need to use something like [PDFReader](https://github.com/vfr/Reader) – iphonic Dec 27 '16 at 10:58
  • Is the no view controller that can be used to achieve for all types of file. – Amon Dec 27 '16 at 11:00
  • You check where the files are downloaded. if you have a path you can open downloaded files using NSFileManager. opening a pdf i think we usually web-view. For an image we can use imageView and for text you can use label or textview. – Subin K Kuriakose Dec 27 '16 at 11:00
  • @Amon ViewController has just `UIView` which works as a container for other controls which you can add into it. You can at least draw Image and Text in UIView directly without any control, but you can't show PDF. – iphonic Dec 27 '16 at 11:02
  • Thanks @GouravJoshi – Amon Dec 27 '16 at 11:07
  • Thanks @SubinKKuriakose – Amon Dec 27 '16 at 11:07
  • @Amin, first you should detect the type of file, btw, if all the files have a suffix at the end? – aircraft Dec 27 '16 at 11:34

2 Answers2

0

I'm not sure if I really understand what your problem is, because it is kind of straight forward. I assume you have downloaded the files to a path of your choice. If not, you most go looking for them, which can be a bit cumbersome. So, you have downloaded your files to the directory of your choice. This can either be a global string or you pass it on to the new view controller.

If it is not a global string, since, for instance, the folder name change each time you download a different file, you have a prepare for segue like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let identifier = segue.identifier {
        switch identifier {
        case "myIdenfifier":
            let vc = segue.destination as! NewViewController
            vc.filePathName = self.filePathName
        default:
            print("I have no idea what's going on.")
        }
    }
}

Now in the new view controller, you have the file. path that you need to retrieve your file. If the file is an image you load the image into memory using, for instance:

let retrievedImage = UIImage(contentOfFile: filePathName)

Where the filePathName is the path name of the file you passed on to your new view controller.

Hope this solves your problem.

MacUserT
  • 1,297
  • 1
  • 17
  • 26
0

You could use QLPreviewController: https://developer.apple.com/reference/quicklook/qlpreviewcontroller

Not sure if it works for plists, though.

crizzis
  • 7,901
  • 2
  • 22
  • 36