简体   繁体   English

如何在我的iOS应用程序中保留HTML文件?

[英]How can I persist an HTML file in my iOS application?

I have registered my app to be able to open HTML files. 我已经注册了我的应用程序,能够打开HTML文件。 I would like the app to keep the last HTML file it opened... If I open a new HTML file, I would like it to overwrite the last one opened. 我希望该应用程序保持打开的最后一个HTML文件...如果我打开一个新的HTML文件,则希望它覆盖打开的最后一个HTML文件。

I have attempted to user the Documents directory and its not working for me. 我试图使用Documents目录,但它对我不起作用。 I have also tried using NSUserDefaults to no avail. 我也尝试使用NSUserDefaults无济于事。 Here is what i currently have: 这是我目前拥有的:

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    ViewController *vc = (ViewController *)self.window.rootViewController;
    [vc loadFileWithUrl:url];

    [[NSUserDefaults standardUserDefaults] setURL:[url fileReferenceURL] forKey:@"amd"];
   return YES;
}

I kill the application entirely, then - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions is called. 我完全杀死了应用程序,然后- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions调用了- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions Here is how that method looks: 该方法的外观如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *url = [[NSUserDefaults standardUserDefaults] URLForKey:@"amd"];
    if (url)
    {
        ViewController *vc = (ViewController *)self.window.rootViewController;
        [vc loadFileWithUrl:url];
    }

    return YES;
}

url is always nil :( url总是nil :(

PS: I have also tried storing the HTML file in its string representation, and I am able to get that to persist in the Documents folder, but when I try to load that string using: PS:我也试图在存储它的字符串表示的HTML文件, 能够得到这在持续Documents文件夹,但是当我尝试加载使用字符串:

[webView loadHTMLString:htmlString]; the page never loads. 该页面永远不会加载。 I think that has something to do with the Javascript but I'm not too sure. 我认为这与Javascript有关,但我不太确定。

The user defaults are saved when the app goes to the background, or when you call -synchronize on the user defaults. 当应用程序进入后台时,或当您调用-synchronize用户默认值时,将保存用户默认值。 When developing, you often don't put the app into background, but just kill it through XCode, in which case nothing gets saved. 在开发时,您通常不将应用程序置于后台,而只是通过XCode将其杀死,在这种情况下,不会保存任何内容。 You can save the user defaults by pressing the home button on your iPhone/iPad, and killing the app in XCode 1second later. 您可以通过按iPhone / iPad上的“主页”按钮并在1秒后使用XCode终止应用程序来保存用户默认设置。

Also, you should put a log statement into your first method, like NSLog(@"saving amd to %@", [url fileReferenceURL]); 另外,您应该在第一个方法中添加一条日志语句,例如NSLog(@"saving amd to %@", [url fileReferenceURL]); to be sure that it really gets saved! 确保它确实得到保存!

It seems that you forgot to call 看来你忘了打电话

[[NSUserDefaults standardUserDefaults] synchronize];

Docs: 文件:

Writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk. 将对永久域的任何修改写入磁盘,并将所有未修改的永久域更新为磁盘上的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM