我正在尝试设置一个应用内购买的应用,可以在购买相应包时下载12个级别的游戏内容。
我坚持如何将下载的图像从缓存文件夹正确移动到Documents文件夹。到目前为止,这是我的代码:
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)
}
}
}
除了从两个do
语句打印的错误之外,其他所有内容都可以正常工作。尝试删除第一个do
块中的任何现有同名文件时,出现以下错误:
Could not remove file “Library” couldn’t be removed because you don’t have permission to access it.
这会导致下一个do语句的下一个错误被打印,因为原始文件无法删除。
为什么会发生这种情况以及如何将下载的文件妥善保存到其他地方的任何想法?感谢。
答案 0 :(得分:1)
我找到了一个合适的解决方案。此代码会将下载的zip文件夹中的所有项目移动到<?php
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.google-analytics.com/collect/v=1&tid=UA-xxxxxxx-1&cid=555&t=pageview&dp=%2Fgeoip.php');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Geoip tracker');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
$arr = array('country' => 'United States', 'city' => 'New York');
if(isset ($_GET['jsonp'])) {
echo $_GET['jsonp'] . '(' . json_encode($arr) . ')';
}
else {
echo json_encode($arr);
}
?>
目录。
Library
我现在可以使用这些文件在SpriteKit中制作SKTextures:
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)
}
}
}