0

I am trying to download one text file from the server and I have completed it. Also, I have stored that file but it seems like it stores at some private location and not able to access that file from other file explorer.

Heres the code for it:

 let documentsUrl =  FileManager.default.urls(for: .documentDirectory,  in: .userDomainMask).first
//        let destinationUrl = documentsUrl!.appendingPathComponent("10xFile.pdf")
    let fileManager = FileManager.default
    do {
        let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
        let fileURL = documentDirectory.appendingPathComponent("10xFile.pdf")

        let dataFromURL = NSData(contentsOf: location)
        dataFromURL?.write(to: fileURL as! URL, atomically: true)

      } catch {
        print(error)
    }

But I need to access this file very easily by any other file explore.

Thanks in advance

niks
  • 11
  • 6
  • what file explorer do you mean? – Anton Novoselov Mar 28 '18 at 08:59
  • Use iCloud. https://stackoverflow.com/questions/32300094/swift-write-save-move-a-document-file-to-icloud-drive – Vyacheslav Mar 28 '18 at 09:06
  • @AntonNovoselov third-party file explorer app. Like we have in android where we can get multiple directory data and files. – niks Mar 28 '18 at 09:17
  • Do you know any of such file explorer app? Does iphone have to be jail broken to be able to install such an explorer? – Anton Novoselov Mar 28 '18 at 10:34
  • @AntonNovoselov Yup we have it for iPad and my iPad is not jail break. – niks Mar 28 '18 at 12:41
  • Could you tell the name of this app? I will try with my device – Anton Novoselov Mar 28 '18 at 12:48
  • @AntonNovoselov you can try this: https://itunes.apple.com/us/app/file-manager-browser/id479295290?mt=8 – niks Mar 29 '18 at 06:33
  • I've installed this app, but I don't understand how can I browse my iPhone file system using it. This app has interface where I can see folders and files created by this app only. Actually, it's the feature of iOS - every app has completely separated filesystem sandbox, and one app can't access files of any another app without special permission created by another app... – Anton Novoselov Mar 29 '18 at 07:08
  • 1
    @AntonNovoselov Ahhh. It means we can not access the file from another app. Thanks a lot for your answer. Might be, I need to open those file from my downloading app only. Isn't it? – niks Mar 29 '18 at 07:12
  • Yes, you can't get right access from another app, but you can pass file to another app from your app. Please have a look at my answer. – Anton Novoselov Mar 29 '18 at 07:44

1 Answers1

0

Actually as I commented - the feature of iOS - every app has completely separated filesystem sandbox, and one app can't access files of any another app without special permission created by another app. But you can use UIActivityViewController to pass your pdf files to another app that can render your pdfs. You can launch UIActivityViewController and pass your pdf file to it. In UIActivityViewController you can choose options - copy file or open in any of apps that can handle pdf file, for example FileManager app. Here is the code sample somewhere based on code you provided in question, I created UIButton and added IBAction for it - UIActivityViewController is presented when user tap button.

override func viewDidLoad() {
        super.viewDidLoad()

        do {

            let documentDirectory = getDocumentsDirectory()

            let fileURL = documentDirectory.appendingPathComponent("10xFile.pdf")

            let dataFromURL = try Data(contentsOf: location!)
            try dataFromURL.write(to: fileURL, options: [])

        } catch {
            print(error)
        }

    }

    func getDocumentsDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentsDirectory = paths[0]
        return documentsDirectory
    }

    @IBAction func actionButtonPressed(_ sender: Any) {

        let documentsDir = getDocumentsDirectory()

        let fileURL = documentsDir.appendingPathComponent("10xFile.pdf")

        let ac = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)

        self.present(ac, animated: true)
    }
Anton Novoselov
  • 769
  • 2
  • 7
  • 19
  • Thank you. But I have created WKWebView and opened that pdf. Also, it's worked well. Thank you for your suggestions it helps a lot to understand iOS architecture. – niks Apr 04 '18 at 12:38