XML ファイルを含む ZIP ファイルをダウンロードします。ファイルを SSZipArchive で解凍し、ディレクトリ /library の下のアプリ サンドボックスにファイルを保存します。
例えば:
/var/mobile/Containers/Data/Application/0A0091C2-7BC5-484F-BDBB-B2ABF305BB09/
完全な絶対ファイル パスは次のとおりです。
/var/mobile/Containers/Data/Application/0A0091C2-7BC5-484F-BDBB-B2ABF305BB09/Libary/bgb.xml
シミュレーターのファイル パス (絶対パス) を /Download/bgb.xml ディレクトリに変更すると、ファイルを読み込むことができるため、問題は奇妙です。
これが私のコードです:
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)
-> true と評価される
NSData(contentsOfFile: filepath)
-> Null/Nil と評価される
この問題は、シミュレーターまたはモバイルからの with パスでのみ発生します。
ダウンロードと解凍のコードは次のとおりです。
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);}