如何在基于 WebKit 的应用程序中启用本地存储?

发布于 2024-10-09 06:27:45 字数 834 浏览 3 评论 0原文

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

我发现

谁能提供一个示例来说明如何为基于 WebKit 的 Cocoa 应用程序启用本地数据库存储?如果是的话非常感谢!

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

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

这样可以,但它需要私有方法 _setLocalStorageDatabasePath,这意味着对我来说没有 App Store。所以我现在修改的问题是:有没有一种方法可以在不使用私有方法的情况下完成这项工作?我在此答案中找到了 WebDatabaseDirectory 首选项键< /a>,它控制数据库的位置。但我在源中的任何地方都找不到本地存储的相应密钥。或者有没有办法让我强制本地存储使用数据库,以及 WebDatabaseDirectory 键?有什么想法吗?

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.

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.

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];

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

会傲 2024-10-16 06:27:45

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

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 年 1 月 29 日他们允许我的申请。 更新:他们还批准了同一应用程序的版本更新,因此它已经经历了两次。

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

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.

奶气 2024-10-16 06:27:45

我将采用 Javascript 到 Objective-C 的桥接方法并将所有内容存储在核心数据中。将 localStorage 设置为 false,然后使用相同的方法构建一个 JS 对象和一个名为“localStorage”的实例。我的 javascript 开发人员不会知道其中的区别,而且我已经不得不对 Air 做同样的事情(基本上)。还有另一种方法可以使 localStorage 保持完整,即使它实际上并未将它们存储在持久数据库中。这些元素可以在 javascript 中迭代并从那里进行操作,但我认为最好简单地用我自己的对象替换该对象。

I'm going to take the Javascript to Objective-C bridge approach and store everything in core data. Set localStorage to false, then build a JS object and an instance named "localStorage" with the same methods. My javascript devs won't know the difference, and I already had to do the same thing with Air (basically). There's another way to leave the localStorage intact even though it doesn't actually store them in a persistent db. The elements can be iterated through in javascript and manipulated from there, but I think it will be better to simply replace the object with my own.

原来分手还会想你 2024-10-16 06:27:45

经过大量的痛苦和挫折后,我找到了一种启用本地存储并使其在应用程序正常运行时持续存在的方法。该解决方案专门针对 OSX,但也可能适用于 iOS。

下载此头文件并将其添加到您的项目中。它不包含在 XCode Webkit 发行版中。

点击下载WebStorageManagerPrivate.h

添加以下行:

static NSString* _storageDirectoryPath();
+ (NSString *)_storageDirectoryPath;

这些行允许您检索 WebKit 本地存储跟踪器数据库的目录位置。这很重要,因为由于 WebKit 中的错误,如果您不将 LocalStorage WebView 文件存储在与跟踪器数据库相同的目录中,那么每次运行应用程序时它们都会被删除。我没有在 WebStorageManager 代码中看到更改单个应用程序的此位置的方法。它始终从用户偏好中读取。

将 WebStorageManagerPrivate.h 包含在您的 appDelegate 中。

#include "WebStorageManagerPrivate.h"

您需要下载 XCode 发行版中未包含的另一个标头并将其包含在项目中。将其另存为 WebPreferencesPrivate.h

单击下载 WebPreferencesPrivate.h

将 WebPreferencesPrivate.h 包含在您的 appDelegate 中。

#include "WebPreferencesPrivate.h"

现在,在 applicationDidFinishLaunching 处理程序中使用以下代码来初始化并启用 LocalStorage。该代码假设您正在使用的 WebView 有一个名为“webView”的 IBOutlet。

    NSString* dbPath = [WebStorageManager _storageDirectoryPath];

    WebPreferences* prefs = [self.webView preferences];
    NSString* localDBPath = [prefs _localStorageDatabasePath];

        // PATHS MUST MATCH!!!!  otherwise localstorage file is erased when starting program
    if( [localDBPath isEqualToString:dbPath] == NO) {
        [prefs setAutosaves:YES];  //SET PREFS AUTOSAVE FIRST otherwise settings aren't saved.
        // Define application cache quota
        static const unsigned long long defaultTotalQuota = 10 * 1024 * 1024; // 10MB
        static const unsigned long long defaultOriginQuota = 5 * 1024 * 1024; // 5MB
        [prefs setApplicationCacheTotalQuota:defaultTotalQuota];
        [prefs setApplicationCacheDefaultOriginQuota:defaultOriginQuota];

        [prefs setWebGLEnabled:YES];
        [prefs setOfflineWebApplicationCacheEnabled:YES];

        [prefs setDatabasesEnabled:YES];
        [prefs setDeveloperExtrasEnabled:[[NSUserDefaults standardUserDefaults] boolForKey: @"developer"]];
#ifdef DEBUG
        [prefs setDeveloperExtrasEnabled:YES];
#endif
        [prefs _setLocalStorageDatabasePath:dbPath];
        [prefs setLocalStorageEnabled:YES];

        [self.webView setPreferences:prefs];
    }

我希望这可以帮助其他已经或仍在努力解决这个问题的人,直到它在 WebKit 中得到正确解决。

After a lot of pain and frustration I found a way to enable local storage and have it persist across application runs properly. This solution is specifically for OSX, but it may be applicable to iOS as well.

Download and add this header file into your project. It's not included in the XCode Webkit distribution.

click to download WebStorageManagerPrivate.h

Add to it, the following lines:

static NSString* _storageDirectoryPath();
+ (NSString *)_storageDirectoryPath;

These allow you to retrieve the directory location of the WebKit local storage tracker database. This is important because due to a bug in WebKit, if you don't store your LocalStorage WebView files in the same directory as the tracker database, they are deleted every other time you run your application. I didn't see a way in the WebStorageManager code to change this location for an individual application. It is always read from the user preferences.

Include the WebStorageManagerPrivate.h in your appDelegate.

#include "WebStorageManagerPrivate.h"

You need to download and include in your project another header not included in XCode distribution. Save it as WebPreferencesPrivate.h

click to download WebPreferencesPrivate.h

Include the WebPreferencesPrivate.h in your appDelegate.

#include "WebPreferencesPrivate.h"

Now use the code below in your applicationDidFinishLaunching handler to initialize and enable LocalStorage. The code assumes you have an IBOutlet named 'webView' for the WebView you are using.

    NSString* dbPath = [WebStorageManager _storageDirectoryPath];

    WebPreferences* prefs = [self.webView preferences];
    NSString* localDBPath = [prefs _localStorageDatabasePath];

        // PATHS MUST MATCH!!!!  otherwise localstorage file is erased when starting program
    if( [localDBPath isEqualToString:dbPath] == NO) {
        [prefs setAutosaves:YES];  //SET PREFS AUTOSAVE FIRST otherwise settings aren't saved.
        // Define application cache quota
        static const unsigned long long defaultTotalQuota = 10 * 1024 * 1024; // 10MB
        static const unsigned long long defaultOriginQuota = 5 * 1024 * 1024; // 5MB
        [prefs setApplicationCacheTotalQuota:defaultTotalQuota];
        [prefs setApplicationCacheDefaultOriginQuota:defaultOriginQuota];

        [prefs setWebGLEnabled:YES];
        [prefs setOfflineWebApplicationCacheEnabled:YES];

        [prefs setDatabasesEnabled:YES];
        [prefs setDeveloperExtrasEnabled:[[NSUserDefaults standardUserDefaults] boolForKey: @"developer"]];
#ifdef DEBUG
        [prefs setDeveloperExtrasEnabled:YES];
#endif
        [prefs _setLocalStorageDatabasePath:dbPath];
        [prefs setLocalStorageEnabled:YES];

        [self.webView setPreferences:prefs];
    }

I hope this helps others have struggled or are still struggling with this issue, until it is fixed properly within WebKit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文