简体   繁体   中英

Can't load with NSData(contentsOfFile: file path) from mobile or simulator filesystem, but it works from OS X filesystem

I download a ZIP file containing an XML file. I unzip the file with SSZipArchive and safe the file in the app sandbox under the directory /library.

For instance:

/var/mobile/Containers/Data/Application/0A0091C2-7BC5-484F-BDBB-B2ABF305BB09/

The full absolute file path is:

/var/mobile/Containers/Data/Application/0A0091C2-7BC5-484F-BDBB-B2ABF305BB09/Libary/bgb.xml

The problem is strange, because if I change the file path (as absolute path) in the simulator to my /Download/bgb.xml directory, I can load the file.

Here is my code:

var filepath = IOOPs().getDirectoryPath().stringByAppendingString("/bgb.xml")
    if(NSFileManager.defaultManager().fileExistsAtPath(filepath)){ 
        print(" File xml existiert")
        var filedata : NSData
        if let tmpData = NSData(contentsOfFile: filepath){
         parser =   NSXMLParser(data: NSData(contentsOfFile: filepath)!)
        }
    }
    parser.delegate = self
    parser.parse()

fileExistsAtPath(file path) -> evaluates true NSData(contentsOfFile: filepath) -> evaluates Null/Nil

The problem only occurs with a with path from the simulator or the mobile.

Here is the code for downloading and unzipping:

 let datei = NSData(contentsOfURL: NSURL(string: "http://www.gesetze-im-internet.de/bgb/xml.zip")!)! 

    print ( getDirectoryPath())
    print("bgb.zip")
    speicherGesetzAufMobilemEndgerät(getDirectoryPath().stringByAppendingString("/bgb.zip"), datei: datei  )
    unzipFile(getDirectoryPath().stringByAppendingString("/bgb.zip"), destinationPath: getDirectoryPath().stringByAppendingString("/bgb.xml") )



 func unzipFile(myZipFile: NSString, destinationPath : NSString){

    let unZipped = SSZipArchive.unzipFileAtPath(myZipFile as! String, toDestination: destinationPath as! String);}

尝试从本地路径创建NSURL

NSData(contentsOfURL: NSURL(fileURLWithPath: filePath))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM