Listing files in iTunes shared folder using Swift

I would like to list all the files in the iTunes shared folder in Table View using Swift.
I check Google and no one talks about it, it seems like this is an unusual need, so if someone can help, it will be very helpful.

EDIT: I found three links about this, but in Objective-C I have no experience in this language. If someone understands this, here are the links.

http://www.exampledb.com/objective-c-get-itunes-file-sharing-folder-files-with-full-path.htm
http://www.infragistics.com/community/blogs/stevez/archive/2013/10/14/ios-objective-c-working-with-files.aspx
http://www.raywenderlich.com/1948/itunes-tutorial-for-ios-how-to-integrate-itunes-file-sharing-with-your-ios-app

+7
xcode ios8 swift xcode6 itunes
source share
1 answer

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.

+9
source share

All Articles