Reputation: 33
I have been working with hosted in-app purchasable content and am running into an issue. I've been able to get the content to download and such but sometimes run into an error. Here is the code I use to access the download:
if let hostedContentPath = contentURL?.appendingPathComponent("Contents", isDirectory: true) {
do {
try FileManager().moveFileToLibrary(hostedPath: hostedContentPath)
onSuccess(contentIdentifier)
} catch let err as NSError {
onError(contentIdentifier, err)
}
}
}
This code is part of an extension of the SKDownload object provided by StoreKit. I have an extension for the FileManager:
func moveFileToLibrary(hostedPath: URL) throws {
let fileManager = FileManager.default
let files = try fileManager.contentsOfDirectory(atPath: hostedPath.relativePath)
for file in files {
let sourcePath: URL = hostedPath.appendingPathComponent(file)
var destinationPath: URL = fileManager.getLibraryPath(folders: [file])
if fileManager.fileExists(atPath: destinationPath.path) {
print("[FileManager] File already present")
return
}
// Move file to Library
try fileManager.moveItem(at: sourcePath, to: destinationPath)
// Set removal from backup
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try destinationPath.setResourceValues(resourceValues)
print("[FileManager] File moved")
}
}
getLibraryPath
simply returns the path of the in-app download in the library directory of the app.
But sometimes I get the error: 'The folder “Contents” doesn’t exist'. However, the download has completed. Has anyone dealt with this issue before? Am I handling something wrong?
Thanks a bunch,
Brendin
Edit: Every so often I get this error when an SKDownload has the failed downloadState: '“mzafbenc.16634573027684271354” couldn’t be moved to “StoreKit” because an item with the same name already exists'. Does anyone know if this is related or if it is something I can fix?
Upvotes: 2
Views: 140
Reputation: 451
Sometimes, after downloading content I got SKDownload with status "failed" and also have error description like your - "mzafbenc.16634573027684271354” couldn’t be moved to “StoreKit” ..."
Simple solution for this is cleaning dir in your app using FileManager
every time in didFinishLaunchingWithOptions
, for next path: Library/Caches/StoreKit
As for "Contents does not exists", make sure, that you finished transaction after all manipulation with downloaded content.
Upvotes: 1