我正在尝试使用iCloud Drive打开,修改和保存文件UIDocument.当我打电话save(to:for:completionHandler:)与文件的位置和使用.forOverwriting的UIDocumentSaveOperation,它的状态完成success = true.但是,iCloud文件(如桌面和iOS文件浏览器中所示)不会更新,重新打开文件时,不会显示更改.我已验证contents(forType:)在保存时返回正确(修改)的文件内容.
(注意:我已经看过这个问题了,但它没有太大帮助)
以下是相关的代码部分:
MainViewController.swift:
var saveFile: SBDocument?
@IBAction func bbiOpen_pressed(_ sender: UIBarButtonItem) {
if saveFile == nil {
let importMenu = UIDocumentMenuViewController(documentTypes: self.UTIs, in: .import)
importMenu.delegate = self
importMenu.popoverPresentationController?.barButtonItem = bbiOpen
self.present(importMenu, animated: true, completion: nil)
} else {
willClose()
}
}
func willClose(_ action: UIAlertAction?) {
if saveFile!.hasUnsavedChanges {
dlgYesNoCancel(self, title: "Save Changes?", message: "Would you like to save the changes to …Run Code Online (Sandbox Code Playgroud) 我检查了最新的Dropbox和Excel for iOS.在Dropbox中,我们得到一个编辑按钮.单击它会打开Excel的扩展,您可以在其中编辑文件.
保存后,更改也会反映在Dropbox文件中.
我想添加这样一个按钮.此外,我想在图像中添加这样一个按钮,以便在可用的"照片编辑"应用程序中打开它们.
如何检查文件(图像,xls,doc或任何其他)是否可以打开进行编辑?
代码到目前为止:
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithURL:url inMode:UIDocumentPickerModeExportToService];
documentPicker.delegate = self;
documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:documentPicker animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
还尝试将模式改为UIDocumentPickerModeMoveToService......

根据apple docs移动本地文档.用户选择外部目的地.文件选择器移动文件; 但是,您仍然可以将文档作为外部文档访问,让用户可以编辑文档.
但我尝试了所有四种模式.没有显示excel选项.
UIDocumentPickerModeImport,
UIDocumentPickerModeOpen,
UIDocumentPickerModeExportToService,
UIDocumentPickerModeMoveToService
Run Code Online (Sandbox Code Playgroud) 我一直在使用URL Resources将缩略图元数据嵌入到我的自定义基于文档的文件中.当我导出自定义文档文件时,在iOS文件应用程序中浏览时,缩略图会很好地显示.
override func fileAttributesToWrite(to url: URL, for saveOperation: UIDocumentSaveOperation) throws -> [AnyHashable : Any] {
var attr: [AnyHashable : Any] = [URLResourceKey.hasHiddenExtensionKey: true]
// Ignore the proper resizing for now.
...
attr[URLResourceKey.thumbnailDictionaryKey] = [
URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey: #imageLiteral(resourceName: "Star")
]
return attr
}
Run Code Online (Sandbox Code Playgroud)
(图标来源:使用你的面包)
但是,当我使用"共享"操作将文件导回到我的应用程序时,除缩略图外Copy to <MyApp>,所有元数据似乎都存在.
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
// Other non-image metadata survives, like creationDate.
if let creationDateOpt = try? url.resourceValues(forKeys: …Run Code Online (Sandbox Code Playgroud) 我有时打开存储在iCloud上的UIDocuments有问题.在检查iCloud是否可用并获取NSMetaDataQuery的结果后,我在应用程序的最开始时调用以下代码.这适用于所有情况的98%.有时(例如有时在重新安装应用程序之后),永远不会调用completionHandler:
FRNBDocument *doc = [[FRNBDocument alloc] initWithFileURL:fileURL];
[doc openWithCompletionHandler:^(BOOL success) {
NSLog(@"... doc: openedWithCompletionHandler");
Run Code Online (Sandbox Code Playgroud)
正如你从代码中看到的那样,我正在初始化一个新的UIDocument实例(FRNBDocument),所以它不能是旧的UIDocument仍然打开的情况,因此这个无法打开.
让应用程序再次运行的唯一方法是杀死它,然后转到settings.app> iCloud> Documents&Data并将其关闭然后再将其打开.当我回到应用程序时,UIDocs将被加载没有问题.
我注意到其他应用程序(例如xTrail by sophiestication)有同样的问题,我只能在完成上述技巧后再次运行它们.
到底是怎么回事?
从iOS 11开始,每次使用UIDocumentAPI 创建新文档时都会遇到以下错误:
[错误]无法获取项目/var/mobile/Containers/Data/Application/XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX/Documents/myDoc-XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX.myFile(n)的属性值.
错误:错误域= NSFileProviderInternalErrorDomain代码= 1
"不允许读者访问该URL."
UserInfo = {NSLocalizedDescription =不允许读者访问该URL.}
不同于类似的问题(1,2,3上SO)在此,我没有使用UIDocumentBrowserViewController.我只是创建一个UIDocument并自己调用save()该Documents目录.我发现的最接近的问题用途UIManagedDocument.但是,就我而言,尽管出现错误消息,仍然可以成功创建和写入文件.
这是我保存例程的要点:
@IBAction func createDoc(_ sender: Any) {
let uuid = UUID().uuidString
let doc = Document(baseName: "myDoc-\(uuid)")
doc.save(to: doc.fileURL, for: .forCreating) { (completed) in
if (completed) {
doc.close(completionHandler: nil)
self.verifyNumberOfFiles()
}
}
}
Run Code Online (Sandbox Code Playgroud)
UIDocument为了简化这个问题,我的子类也几乎是空白的:
class Document: UIDocument {
let fileExtension = "myFile"
override …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用iCloud Documents来存储我的iOS应用程序中的XML文件.一切似乎工作正常(我可以无错误地编写和阅读文件),除了我的App文件不会出现在icloud.com和developer.icloud.com以及iCloud Drive文件夹中的Windows PC上的iCloud Documents中.我在模拟器中运行应用程序,并在真实设备上使用TestFlight进行测试.我安装了最新版本的iCloud for Windows 4.0.该应用程序是在Xcode 6中创建的.
有谁知道如何使文件出现在iCloud Documents中?
我用来保存文件的代码:
NSLog(@"Syncing with iCloud");
NSURL *ubiq = [filemgr URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
NSURL *ubiquitousPackage = [ubiq URLByAppendingPathComponent:@"Documents" isDirectory:YES];
if ([filemgr fileExistsAtPath:[ubiquitousPackage path]] == NO)
[filemgr createDirectoryAtURL:ubiquitousPackage
withIntermediateDirectories:YES
attributes:nil
error:nil];
ubiquitousPackage = [ubiquitousPackage URLByAppendingPathComponent:@"data.pxa"];
DataFile *file = [[DataFile alloc] initWithFileURL:ubiquitousPackage];
file.xmlContent = doc.XMLString;
[file saveToURL:[file fileURL] forSaveOperation:UIDocumentSaveForCreating | UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
if (success) {
NSLog(@"Synced with iCloud: %@", [ubiquitousPackage path]);
} else {
NSLog(@"Syncing with iCloud failed");
}
}]; …Run Code Online (Sandbox Code Playgroud) 我正在尝试UIDocument使用a 导出我的子类UIDocumentPickerViewController.子类将数据写入a FileWrapper并且其UTI符合com.apple.package.
但是,所提供的文档选择器显示"iCloud Drive中的文档无法使用,因为iCloud Drive设置已禁用."

正如我从导出的容器包中看到的那样,文档已成功写入缓存.
当我更改文档子类和自定义UTI以符合单个文件(例如public.plain-text)时,文档选择器工作正常,我可以导出文件.所以问题似乎与文档类型或导出的UTI有关.
我做错了什么或这是一个错误?
的Info.plist
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>Custom Doc</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.zxzxlch.documentsandbox.customdoc</string>
</array>
<key>LSTypeIsPackage</key>
<true/>
</dict>
</array>
...
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>com.apple.package</string>
</array>
<key>UTTypeDescription</key>
<string>Custom Doc File</string>
<key>UTTypeIdentifier</key>
<string>com.zxzxlch.documentsandbox.customdoc</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>zzz</string>
</array>
</dict>
</dict>
</array>
Run Code Online (Sandbox Code Playgroud)
CustomDocument.swift
private let textFilename = "contents.txt"
class CustomDocument: UIDocument {
var content = "Test"
override func load(fromContents contents: Any, …Run Code Online (Sandbox Code Playgroud) 我想让我的应用程序将它创建的文档保存到iCloud Drive,但我很难跟上Apple写的内容.这是我到目前为止所做的,但我不确定从哪里开始.
UPDATE2
我的代码中有以下内容手动将文档保存到iCloud Drive:
- (void)initializeiCloudAccessWithCompletion:(void (^)(BOOL available)) completion {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.ubiquityURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (self.ubiquityURL != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"iCloud available at: %@", self.ubiquityURL);
completion(TRUE);
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"iCloud not available");
completion(FALSE);
});
}
});
}
if (buttonIndex == 4) {
[self initializeiCloudAccessWithCompletion:^(BOOL available) {
_iCloudAvailable = available;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:selectedCountry];
NSURL* url = [NSURL fileURLWithPath: pdfPath]; …Run Code Online (Sandbox Code Playgroud) 我已经尝试了两天多来写一个文件到iCloud驱动器.我尝试直接编写一个简单的文本文件,然后在本地移动它,使用UIDocumentMenuViewController等.我没有得到任何错误的代码和单步执行调试器,它看起来很成功,但当我检查文件是否存在或至少iCloud目录,那里什么都没有.我尝试了模拟器和我的iPhone,触发了iCloud同步,以及我能想到的其他一切.
我的主要目标是简单地将文本文件写入iCloud驱动器,后来将成为"数字"文件
我已经设置了我的plist文件和我的权利:
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.paul.c.$(PRODUCT_NAME:rfc1034identifier)</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>myCloudTest</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
我还提到了捆绑版本,如下所述:将iOS 8文档保存到iCloud Drive
我已经尝试了几十个没有运气的教程.我的最新代码基于此示例:https://medium.com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1
这是我的代码:
@IBAction func ExportFile(sender: AnyObject) {
var error:NSError?
let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent("myCloudTest")
//is iCloud working?
if iCloudDocumentsURL != nil {
//Create the Directory if it doesn't exist
if (!NSFileManager.defaultManager().fileExistsAtPath(iCloudDocumentsURL!.path!, isDirectory: nil)) {
//This gets skipped after initial run saying directory exists, but still don't see it on iCloud
NSFileManager.defaultManager().createDirectoryAtURL(iCloudDocumentsURL!, withIntermediateDirectories: true, attributes: nil, error: nil)
}
} …Run Code Online (Sandbox Code Playgroud) 更新:Xcode 9.3修复了它.
在中创建UIDocument文件时 UIDocumentBrowserViewController
[默认] [错误]无法解析书签.错误:错误域= NSFileProviderInternalErrorDomain代码= 1"不允许读者访问该URL." UserInfo = {NSLocalizedDescription =不允许读者访问该URL.}
[DocumentManager]无法从bookmarkableString创建URL(错误域= NSFileProviderInternalErrorDomain Code = 1"不允许读者访问URL."UserInfo = {NSLocalizedDescription =不允许读者访问URL.})
代码在Xcode 9.1中运行良好,但在Xcode 9.2中失败.与wwdc 2017几乎相同的代码.
func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {
let newDocumentURL: URL? = R.file.templateDocument()
// Set the URL for the new document here. Optionally, you can present a template chooser before calling the importHandler.
// Make sure the importHandler is always called, even if the user cancels the creation request.
if newDocumentURL …Run Code Online (Sandbox Code Playgroud) uidocument ×10
ios ×9
icloud ×6
xcode ×3
swift ×2
uti ×2
core-data ×1
excel ×1
icloud-drive ×1
ios11 ×1
ios8 ×1
iphone ×1
metadata ×1
objective-c ×1
save ×1
thumbnails ×1
url ×1