我正在开发一个 iOS 应用,其中包含应用内购买,该应用内购买由下载到用户设备的 PNG 文件组成。
从我查看日志语句可以看出,文件已成功下载到此目录:
/var/mobile/Containers/Data/Application/ABC43D27-B79C-486C-AE39-F34BF6EF8658/Documents/
如何将文件移动到用户设备上用户能够访问文件的位置,例如用户的 iCloud 存储空间?
如果这会有用,下面是我下载文件的代码。
func paymentQueue(queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) {
for download:AnyObject in downloads {
if let down:SKDownload = download as? SKDownload {
switch down.downloadState {
case .Active:
NSLog("progress %f", down.progress)
NSLog("remaining %f", down.timeRemaining)
NSLog("Active")
break
case .Cancelled:
NSLog("%@", "download cancelled")
break
case .Failed:
NSLog("%@", "download failed")
textView.text = "Download Failed"
break
case .Finished:
NSLog("Finished")
let source = down.contentURL?.relativePath
//var dict = NSMutableDictionary(contentsOfFile: (source! as NSString).stringByAppendingPathComponent("ContentInfo.plist"))
let pathToDownloadsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
//let pathToDownloadsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let content:String =
(pathToDownloadsFolder as NSString).stringByAppendingPathComponent("bonus_brune_image.png")
//let content:String = pathToDownloadsFolder.URLByAppendingPathComponent("bonus_brune_image.png")
self.copyPathToDownloadsFolder(source!, target: content)
SKPaymentQueue.defaultQueue().finishTransaction(down.transaction)
break
case .Paused:
NSLog("%@", "download paused")
break
case .Waiting:
NSLog("%@", "download waiting")
break
}
}
}
}
func copyPathToDownloadsFolder(source:String, target:String) {
NSLog("Source is %@", source)
NSLog("Target is %@", target)
let filemgr:NSFileManager = NSFileManager.defaultManager()
do {
try filemgr.copyItemAtPath(source, toPath: target)
NSLog("Success: file is now in %@", target)
} catch let e as NSError {
NSLog(String(e))
}
}
最佳答案
所以我发现解决方案是这样的:使用iTunes 文件共享。
要启用 iTunes 文件共享,请在 info.plist
中将 key UIFileSharingEnabled
设置为 true
。
然后当用户将设备连接到 iTunes 并在 iTunes 中查看已安装的应用程序时,您的应用程序将列在“文件共享”应用程序中,并且存储在该应用程序的 Documents
目录中的文档将可供用户保存。
关于ios - 在 iOS 设备上访问应用内购买,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36244120/