如何在基于WebKit的应用程序中启用本地存储? [英] How do I enable Local Storage in my WebKit-based application?

查看:184
本文介绍了如何在基于WebKit的应用程序中启用本地存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌入WebKit WebView的Cocoa / Objective-C应用程序。我需要打开数据库支持和本地存储。我知道它可以完成 - 我在Safari中工作 - 但我找不到如何在我自己的应用程序中设置它的示例。

I have a Cocoa/Objective-C application which embeds a WebKit WebView. I need to turn on database support and local storage. I know it can be done--I have it working in Safari--but I can't find an example of how to set this up in my own application.

我发现这个(未答复的)SO问题提供了一个例子,但正如原始海报所提到的那样,它不起作用。事实上,他使用的方法(setDatabasesEnabled,setLocalStorageEnabled)没有在我的WebKit.framework(Xcode 3.2.5)中定义,尽管如果我自己定义它们似乎存在。

I found this (unanswered) SO question which provides an example but, as the original poster mentions, doesn't work. And in fact, the methods he uses (setDatabasesEnabled, setLocalStorageEnabled) aren't defined in my WebKit.framework (Xcode 3.2.5), although they appear to exist if I define them myself.

任何人都可以提供一个如何为基于WebKit的Cocoa应用程序启用本地数据库存储的示例吗?非常感谢,如果是这样的话!

Can anyone provide an example of how to enable local database storage for a WebKit-based Cocoa application? Many thanks if so!

更新:我有一些工作......我对数据库与本地存储感到困惑,这显然是完全不同的事情。这是有效的代码:

Update: I've got something working...I was confused by "databases" vs. "local storage", which are apparently quite different things. Here's the code that works:

WebPreferences* prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:@"~/Library/Application Support/MyApp"];
[prefs setLocalStorageEnabled:YES];

这样可行,但它需要私有方法_setLocalStorageDatabasePath,这意味着我没有App Store。所以我现在修改过的问题是:有没有办法让这个工作没有使用私有方法?我在此答案中找到了 WebDatabaseDirectory首选项键,它控制数据库的去向。但我无法在源中的任何位置找到相应的本地存储密钥。或者有没有办法强制本地存储使用数据库,所以WebDatabaseDirectory键?任何想法?

So that works, but it requires the private method _setLocalStorageDatabasePath, which means no App Store for me. So my amended questions is now: is there a way to make this work without using a private method? I found the WebDatabaseDirectory preference key in this answer, which controls where databases go. But I couldn't find a corresponding key for local storage anywhere in the sources. Or is there a way for me to force local storage to use the database, and so the WebDatabaseDirectory key? Any ideas?

推荐答案

我使用此代码将应用程序提交到Mac App Store,Apple批准了它:

I submitted an app using this code to the Mac App Store, and Apple approved it:

Objective-C

WebPreferences* prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:@"~/Library/Application Support/MyApp"];
[prefs setLocalStorageEnabled:YES];

Swift 3

var prefs: WebPreferences? = webView.preferences
prefs?._setLocalStorageDatabasePath("~/Library/Application Support/MyApp")
prefs?.localStorageEnabled = true

他们是否会继续批准,我不知道,但截至2011-01-29,他们允许我申请。 更新:他们还批准了对同一个应用的版本更新,因此已经过两次。

Whether they will continue to approve that, I don't know, but they allowed it for my application as of 2011-01-29. Update: They also approved a version update to the same app, so it has gone through twice.

这篇关于如何在基于WebKit的应用程序中启用本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆