问题描述
不管我尝试什么,这都行不通...
No matter what I try, this isn't working...
我正在尝试使用UIDocument
在本地保存我的(文本)文件.当我创建文件(即未加载)时,它可以保存. 但是,如果我先打开然后加载文件,然后再保存,则文件将无法保存.
I am trying to use UIDocument
to save my (text) files locally. When I create a file (i.e. it isn't loaded), it saves fine. However, if I load my file by opening it and then save it, it simply won't save.
编辑:当应用进入后台时,我尝试保存我的文档.但是,当我进入前台时,该应用程序似乎实际上完成了保存"文档.我的应用程序在后台时是否不允许保存文档?
EDIT: I try to save my documents when the app enters the background. However, it seems that the app actually "finishes saving" the documents when I enter the foreground. Is my app not allowed to save documents while in the background?
我检查了我的URL,它们都是有效的.
I've checked my URLs and they're all valid.
这是从头开始制作文档的代码:
Here is the code for making the document from scratch:
document = [[MyDocumentClass alloc] initWithFileURL:fileURL];
fileURL
是先前制作的.加载时,我不会打开或创建它,只是在用户退出应用程序时保存它.
fileURL
was made previously.When loading I don't open or create it, I simply save it when the user quits the app.
如果文件已经在磁盘上:
If the file already exists on disk:
以下是打开代码:
[document openWithCompletionHandler:^(BOOL success){
if (!success) {
NSLog(@"FAILED TO OPEN DOCUMENT");
}
// Code to handle document's data
}];
并保存:
[document saveToURL:[NSURL fileURLWithPath:path] forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success){
if (!success || document.documentState == UIDocumentStateSavingError) {
NSLog(@"FAILED TO CLOSE DOCUMENT");
}
}];
我的文档在contentsForType: error:
中正确编码并提供了数据,因为它在初次创建时可以正确保存.
My document encodes and provides its data correctly in contentsForType: error:
, given that it saves correctly when first being created.
我在handleError: userInteractionPermitted:
中没有任何错误
有什么想法吗?
N.B.(我将数据保存在applicationDidEnterBackground:
saveToURL: forSaveOperation: completionHandler:
的完成处理程序永远不会被调用...
And the completion handler for saveToURL: forSaveOperation: completionHandler:
is never called...
推荐答案
您首先在某处创建了文档吗?
Have you first created the document somewhere?
例如,用于创建:
[document saveToURL:[NSURL fileURLWithPath:path] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
if (!success || document.documentState == UIDocumentStateSavingError) {
NSLog(@"FAILED TO CLOSE DOCUMENT");
}
}];
并覆盖:
[document saveToURL:[NSURL fileURLWithPath:path] forSaveOperation: UIDocumentSaveForOverwriting completionHandler:^(BOOL success){
if (!success || document.documentState == UIDocumentStateSavingError) {
NSLog(@"FAILED TO CLOSE DOCUMENT");
}
}];
这篇关于UIDocument不能保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!