Unable to load NSData (contentOfFile: file path) from a mobile or simulated file system, but it works from the OS X file system

I am downloading a zip file containing an xml file. I will unzip the file using SSZipArchive and protect the file in the application sandbox in the directory / library.

For example:

/ var / mobile / Containers / data / applications / 0A0091C2-7BC5-484F-BDBB-B2ABF305BB09 /

Full path to the file:

/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 an absolute path) in the simulator to the /Download/bgb.xml directory, I can upload 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)→ takes the value Null / Nil

The problem only occurs if there is a path from the simulator or mobile device.

:

 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);}
+4
1

NSURL :

NSData(contentsOfURL: NSURL(fileURLWithPath: filePath))
0

Source: https://habr.com/ru/post/1616388/


All Articles