Search code examples
uiwebviewswift3nsfilemanagerfile-exists

To check file Existence at filepath (Handling exception) in Swift 3


I am trying to check if file exist at the path in swift 3 but it always shows "NO FILE EXIST"

I have also tried it with atPath: self.strTitle+"/back/index.html" and even this doesn't work.

here strTitle is a file path located inside asset folder and is read through an array. There lies two pages one on the back side and one on the front. So there is front for all but back pages are limited to few. And this is where it breaks.

print("STR TITLE::  ",self.strTitle) .   // STR TITLE = assets/a6th_nerve_palsy
let fileManager : FileManager   = FileManager.default

if fileManager.fileExists(atPath: self.strTitle+"/back/index"){
    print("FILE EXIST")

    }else{
        print("NO FILE EXIST")
    }

Thanks in advance :)


Solution

  • After debugging and with help of @LeoDabus I could figure out. It could directly be done to check file path existence.

    let bundle = Bundle(for: PageContentViewController.self)
    let filePath = bundle.path(forResource: self.strTitle+"/back/index", ofType: "html")
    
            if filePath == nil{
                print("FILE NOT FOUND")
                // Show No page as page do not exist
            } else {
                print("FILE FOUND")
                // Show Back page
            }
    

    Thanks all. :)