Based on this objective-C tutorial http://mobiforge.com/design-development/importing-exporting-documents-ios , I created three methods: listFilesFromDocumentsFolder , which returns a list of the names of all the documents that I have in the iTunes shared folder for applications and loadFileFromDocumentsFolder , which loads the URL for the given file name and passes the handleDocumentOpenUrl URL to load the file into UIWebView . Find below three methods. You can also download the project from github: https://github.com/Euniceadu/Load-Shared-Documents
listFilesFromDocumentsFolder
func listFilesFromDocumentsFolder() { var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) var documentsDirectory : String; documentsDirectory = paths[0] as String var fileManager: NSFileManager = NSFileManager() var fileList: NSArray = fileManager.contentsOfDirectoryAtPath(documentsDirectory, error: nil)! var filesStr: NSMutableString = NSMutableString(string: "Files in Documents folder \n") for s in fileList { filesStr.appendFormat("%@", s as String) } self.displayAlert(filesStr) }
loadFileFromDocumentsFolder
func loadFileFromDocumentsFolder(fileName: String) { var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) var documentsDirectory : String; documentsDirectory = paths[0] as String var filePath: String = documentsDirectory.stringByAppendingPathComponent(fileName); var fileUrl: NSURL = NSURL(fileURLWithPath: filePath); self.handleDocumentOpenURL(fileUrl) }
handleDocumentOpenUrl
func handleDocumentOpenURL(url: NSURL) { var requestObj = NSURLRequest(URL: url) webView.userInteractionEnabled = true webView.loadRequest(requestObj) }
Hope this helps.
euniceadu
source share