使用Swift列出iTunes共享文件夹中的文件

Viv*_*enG 9 xcode itunes swift xcode6 ios8

我想使用Swift在" 表格视图 "中列出我的iTunes共享文件夹中的所有文件. 我检查谷歌,没有人谈论它,看起来这是一个不寻常的需求,所以如果有人可以帮助它将是非常有帮助的.

编辑:我发现三个链接谈论它,但在Objective-C,我没有这种语言的经验.如果有人理解这一点,这里是链接.

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-分享与-您- IOS-应用

eun*_*adu 10

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 documents I have in the apps iTunes shared folder and loadFileFromDocumentsFolder which loads the url for a given filename and passes the url to handleDocumentOpenUrl to load the file on a UIWebView. Find below the 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)
}
Run Code Online (Sandbox Code Playgroud)

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)

}
Run Code Online (Sandbox Code Playgroud)

handleDocumentOpenUrl

func handleDocumentOpenURL(url: NSURL) {
    var requestObj = NSURLRequest(URL: url)
    webView.userInteractionEnabled = true
    webView.loadRequest(requestObj)
}
Run Code Online (Sandbox Code Playgroud)

Hope this helps.