我知道该文件存在,因为我可以下载它,但我需要检查它是否存在。我尝试过使用
[NSFileManager contentsOfDirectoryAtPath:error:]
Run Code Online (Sandbox Code Playgroud)
但它给了我空。我不明白为什么这是因为我仍然可以下载我正在寻找的文件。也许这是一个不正确的 URL,但我正在使用的 URL 是我在创建我要查找的 UIDocument 时打印的 URL。也许我使用了错误的方法?
编辑:
我也尝试过使用 NSMetadataQuery,我可以让它返回通知,但它永远不会有结果,即使我可以显式下载我正在查找的文件。
我不小心删除了 iPhone 上的一个应用程序。数据也丢失了。数据仅备份在 iCloud 上。如何在不恢复整个系统的情况下使用 iCloud 中的数据恢复该应用程序?
谢谢
我在应用程序中有一些可下载的内容(例如:游戏角色图像),在 iOS 中它们被保存到路径 NSCachesDirectory,我遵循 iOS 的指导方针,可下载内容应保存在 NSCachesDirectory 中,这是一个缓存目录。如果我应该将文件保存到 android 端的 getCacheDir() 中,我会很挣扎。
起初,我认为 iOS 中的缓存文件夹应该与 android 中的缓存文件夹等效(或相似),但在查看了一些文档后,我怀疑它们在功能上是否相同:
1.iOS 建议将一些可下载的内容保存在缓存文件夹中,此类内容更多是在 tmp 中持久化临时文件。但是在 android 中,文档说 getCacheDir() 应该存储缓存文件而不是持久文件,这让我怀疑 android 缓存文件夹更像 iOS 中的 tmp 文件夹
2.我在iOS缓存文件上找不到任何大小限制指南,但android方面说它应该有合理的大小限制,比如1 MB(我认为我的内容会远远超过1 MB,比如50到100 MB)。
所以我的问题是,iOS 缓存文件夹在功能上是否等同于 android 中的缓存文件夹?在我在 iOS 端使用 NSCachesDirectory 的地方在 android 端使用 getCacheDir() 是否正确?如果没有,当我在 iOS 端使用 NSCachesDirectory 时,我应该在 android 端使用哪个路径?
我正在寻找 watchkit 扩展来从 iCloud 文档存储中读取文件。我从 iOS 应用程序编写了该文件,该应用程序也可以读取它。我正在利用共享类,因此代码是相同的。问题是在手表上,云容器的 URL 返回 nil。
static func readFromFile(fileName:String) -> String?
{
let fileManager = FileManager.default
var cloudURL = fileManager.url(forUbiquityContainerIdentifier: nil)
cloudURL = cloudURL?.appendingPathComponent("Documents/\(fileName)")
do
{
return try String(contentsOf: cloudURL!)
} catch {
print (error)
return nil
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,cloudURL在手表上是nil,但在手机上不是。为此,forUbiquityContainerIdentifier我检查了手表和手机使用相同的标识符。我还尝试直接输入名称,而不是使用 nil 并让它从权利中获取它。容器的格式:
iCloud.com.company.app
Run Code Online (Sandbox Code Playgroud)
据我了解,Watch OS 3 应该能够使用 iCloud。
更新:
我打印出来let token = fileManager.ubiquityIdentityToken并得到以下内容:
WatchKit Extension[390:687462] [default] [ERROR] 获取 ubiquityIdentityToken 时出错:Error Domain=NSCocoaErrorDomain Code=4099 “与名为 com.apple.bird.token 的服务的连接已失效。” UserInfo={NSDebugDescription=与名为 com.apple.bird.token 的服务的连接已失效。}
我正在尝试在我的应用程序的 iCloud 无处不在容器中下载一个目录,并且我想在下载完成时调用一个函数。我可以得到这样的状态:
func synciCloud(){
do { try FileManager.default.startDownloadingUbiquitousItem(at: documentsPath)
do {
let status = try documentsPath.resourceValues(forKeys: [.ubiquitousItemDownloadingStatusKey])
print(status)
}
catch let error { print("Failed to get status: \(error.localizedDescription)") }
}
catch let error { print("Failed to download iCloud Documnets Folder: \(error.localizedDescription)") }
}
Run Code Online (Sandbox Code Playgroud)
控制台打印:
URLResourceValues(_values: [__C.NSURLResourceKey(_rawValue: NSURLUbiquitousItemDownloadingStatusKey): NSURLUbiquitousItemDownloadingStatusCurrent], _keys: 设置([__C.NSURLResourceKey(_rawValue: NSURLUbiquitousItemDownloadingStatusKey)]))
我感到困惑的是 NSMetadata 的语法以及如何执行我的函数。我的第一个想法是做类似下面代码的事情,但我不知道如何去做:
if status.ubiquitousItemDownloadingStatus == ???? { function() }
Run Code Online (Sandbox Code Playgroud)
任何建议,将不胜感激。
谢谢。
我正试图从我的一个应用程序将文件推送到iCloud,但有时无法跟踪文件是否已经在iCloud上.在将文件标记为无处不在之后,我有一种方法可以通过NSMetadataQuery定期检查文件的状态.当我得到结果时,我使用这些键作为文件的状态:
NSNumber *isUploaded = [file valueForAttribute:NSMetadataUbiquitousItemIsUploadedKey];
NSNumber *isUploading = [file valueForAttribute:NSMetadataUbiquitousItemIsUploadingKey];
NSNumber *uploadPercent = [file valueForAttribute:NSMetadataUbiquitousItemPercentUploadedKey];
Run Code Online (Sandbox Code Playgroud)
这似乎有时有效,但在其他时候isUploaded值为0,isUploading值也为0!uploadPercent值实际上始终为0.当我进入设备的"设置"应用程序并检查我的应用程序的文件时,文件已经列在那里,所以我不确定为什么'NSMetadataUbiquitousItemIsUploadedKey'键会返回0?
任何帮助表示赞赏!谢谢!
我正在寻找一种更好的方法从我的iCloud容器下载一堆文件到我的沙箱.这是我目前使用的:
for (FileAttachments *fileLink in items) {
NSURL *cloudFileURL = [fileLink urlForCloudAttachmentWithName];
NSURL *fileURL = [backupCloudLocalDirectoryURL URLByAppendingPathComponent: fileLink.fileName];
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fileCoordinator coordinateReadingItemAtURL:fileURL options:NSFileCoordinatorReadingWithoutChanges error:&error
byAccessor:^(NSURL *newURL) {
NSError *blockError = nil;
[fileManager copyItemAtURL:cloudFileURL toURL:fileURL error:&blockError];
}];
}
}
Run Code Online (Sandbox Code Playgroud)
以这种方式复制iCloud项目有什么问题吗?在制作中,我有一些用户抱怨所有文件都没有下载.是否更好地使用NSFileManager's startDownloadingUbiquitousItemAtURL:error?如果是这样,为什么?谢谢.
我无法将我的CoreData同步到iCloud I Keep在控制台中输入错误说
2015-02-22 23:09:31.229 Newstron[1711:233218] -[PFUbiquitySetupAssistant finishSetupWithRetry:](826): CoreData: Ubiquity: <PFUbiquitySetupAssistant: 0x7fbc6357e580>: Retrying after delay: 60
Error Domain=NSCocoaErrorDomain Code=134080 "The operation couldn’t be completed. (Cocoa error 134080.)" UserInfo=0x7fbc637794f0 {Reason=Didn't get a container URL back from URLForUbiquityContainerIdentifier:, giving up now. Please ensure the application is signed with the proper entitlements to read from the container., NSPersistentStoreUbiquitousContainerIdentifierKey=null}
我已经在AppDelegate.swift中获得了代码
// MARK: - Core Data stack
func observeCloudActions(persistentStoreCoordinator psc: NSPersistentStoreCoordinator?) {
// iCloud notification subscriptions
let nc = NSNotificationCenter.defaultCenter();
nc.addObserver(
self,
selector: "storesWillChange:",
name: …Run Code Online (Sandbox Code Playgroud) 是否有更好的解决方案(以下可能的解决方法)为iCloud/CloudKit用户获取UUID?
因此,可以检测用户是否已使用相同的iCloud帐户执行某项操作(在其他设备上).
可能的解决方法:
我将文件存储在iCloud中作为备份方式.上传和下载文件似乎工作正常,但如果我删除设备上的应用程序并在同一台设备上重新安装应用程序,我就不会再看到上传到iCloud的文件(即使在删除应用程序之前从该设备上传).所有安装中的ubiquityIdentityToken都是相同的.我不是试图在设备之间同步,只是存储和检索.我可以在\ settings\icloud\manage storage \中查看文件,但不能通过运行此代码:
func createListOfSQLBaseFilesIniCloudDocumentsDirectory() -> [String]{
let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
var iCloudBackupSQLFiles = [String]()
do{
let directoryContents = try FileManager.default.contentsOfDirectory(at: iCloudDocumentsURL!, includingPropertiesForKeys: nil, options: [])
for myFile in directoryContents {
if myFile.pathExtension == "sqlite" {
let fileWithExtension = myFile.lastPathComponent
//backupSQLFiles is an array of full file names - including the extension
iCloudBackupSQLFiles.append(fileWithExtension)
}//if myFile
}//for in
} catch let error as NSError {
print("Unresolved error \(error), \(error.userInfo)")
}//do catch
return iCloudBackupSQLFiles
}//createListOfSQLBaseFilesIniCloudDocumentsDirectory
Run Code Online (Sandbox Code Playgroud)
任何指导将不胜感激.Swift 3,iOS 10,Xcode 8