2016-04-07 15 views
1

私は、それぞれのパックを購入したときにゲームの12レベルのコンテンツをダウンロードするアプリ内購入でアプリをセットアップしようとしています。iOSのキャッシュからApp App Purchaseのダウンロードファイルを正しく移動するには?

ダウンロードしたイメージをキャッシュフォルダからドキュメントフォルダに適切に移動する方法について説明しました。ここに私のコードは、これまでのところです:

func processDownload(sender: NSURL) { 

    //Convert URL to String, suitable for NSFileManager 
    var path:String = sender.path! 
    path = path.stringByAppendingPathComponent("Contents") 

    //Makes an NSArray with all of the downloaded files 
    let fileManager = NSFileManager.defaultManager() 
    var files: NSArray! 
    do { 
     files = try fileManager.contentsOfDirectoryAtPath(path) 
    } catch let err as NSError { 
     print("Error finding zip URL", err.localizedDescription) 
    } 

    //For each file, move it to Library 
    for file in files { 

     let pathSource: String = path.stringByAppendingPathComponent(file as! String) 
     let pathDestination: String = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0] 

     //Remove destination files b/c not allowed to overwrite 
     do { 
      try fileManager.removeItemAtPath(pathDestination) 
     }catch let err as NSError { 
      print("Could not remove file", err.localizedDescription) 
     } 

     //Move file 
     do { 
      try fileManager.moveItemAtPath(pathSource, toPath: pathDestination) 
      print("File", file, "Moved") 
     }catch let err as NSError { 
      print("Couldn't move file", err.localizedDescription) 
     } 
    } 
} 

すべてが、実際に2つのdo文から印刷されたエラーを除くだけで正常に動作します。最初doブロックに同じ名前の既存のファイルを削除しようとすると、私は次のエラーを取得する:

Could not remove file “Library” couldn’t be removed because you don’t have permission to access it. 

これは、その後、元を削除することができませんでしたので、印刷するには、次do文から次のエラーが発生します。

なぜこれが起こっているのか、ダウンロードしたファイルをどこに保存するのかについてのアイデアはありますか?ありがとう。

+0

エラーは、このあなたはおそらく、許可されていないコースの「ライブラリ」フォルダを!、削除しようとしている、と言いますそこにサブフォルダを削除したいですか?あなたは確認できますか? – Shripada

+0

ああ、私は今それを見る。私は現在の "ファイル"項目がすでに存在していたかどうかを確認しようとしていました。 – jwade502

答えて

0

私は適切な解決策を見つけました。このコードは、ダウンロードしたzipフォルダ内のすべての項目をLibraryディレクトリに移動します。

func processDownload(sender: NSURL) { 

    //Convert URL to String, suitable for NSFileManager 
    var path: String = sender.path! 
    path = path.stringByAppendingPathComponent("Contents") 

    //Makes an NSArray with all of the downloaded files 
    let fileManager = NSFileManager.defaultManager() 
    var files: NSArray! 
    do { 
     files = try fileManager.contentsOfDirectoryAtPath(path) 
    } catch let err as NSError { 
     print("Error finding zip URL", err.localizedDescription) 
    } 

    //For each file, move it to Library 
    for file in files { 

     let currentPath: String = path.stringByAppendingPathComponent(file as! String) 
     var pathDestination: String = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0] 
     pathDestination = pathDestination.stringByAppendingPathComponent(file as! String) 

     //Move file 
     do { 
      try fileManager.moveItemAtPath(currentPath, toPath: pathDestination) 
      print("File", file, "Moved") 
     }catch let err as NSError { 
      print("Couldn't move file", err.localizedDescription) 
     } 
    } 
} 

私は今、そうのように、これらのファイルをSpriteKitベースにSKTexturesを行うことができます。

var rippleTex = SKTexture(image: UIImage(contentsOfFile: NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0].stringByAppendingPathComponent("P06_ripple.png"))!) 
関連する問題