【问题标题】:localStorage UIWebView iPhone / iPad saving or reading not working.. sometimes?localStorage UIWebView iPhone / iPad 保存或读取不起作用.. 有时?
【发布时间】:2011-06-30 14:16:00
【问题描述】:

我有 2 个 HTML5 应用程序,它们使用:

 localStorage[key] = value;

保存数据(我尝试了其他 localStorage 方法,例如 localStorage.keyName = "Bla" 有同样的问题)。这些文件被添加为引用文件(蓝色文件夹),并带有此代码以显示在 UIWebView 上:

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" 
                                                     ofType:@"html" 
                                                inDirectory:@"/some_path" ];

NSString *html = [NSString stringWithContentsOfFile:htmlPath 
                                           encoding:NSUTF8StringEncoding 
                                              error:nil];

[webView loadHTMLString:html 
                baseURL:[NSURL fileURLWithPath:
                         [NSString stringWithFormat:@"%@/some_path/", 
                          [[NSBundle mainBundle] bundlePath]]]];
webView.scalesPageToFit = YES;

奇怪的是 localStorage (javascript) 可以在一个应用程序上运行,而不能在另一个应用程序上运行。在它不起作用的地方,我尝试了一个 try/catch 代码来获取错误,并得到一个 DOM 18 Security_Err。听起来它不允许我访问它,因为文件是本地的。如果两个应用程序都是这种情况,那将是有意义的。

我不知道有什么不同,因为它们中的代码几乎相同。我没有为 UIWebView 添加任何特殊功能来禁用任何类型的特殊安全限制。

我真的不知道发生了什么。唯一不同的是 HTML/JS/CSS 方面。我不认为这个问题是由 Cocoa Touch 代码引起的,因为我确实复制了项目来创建第二个应用程序。

提前致谢,如果您需要更多信息,请随时询问。我无法在 Google 或此处找到任何解决方案。似乎缺少有关 localStorage 键/值的信息。这是有道理的,因为它很容易使用,但仍然如此。其他人有这个问题吗?

【问题讨论】:

    标签: javascript iphone ios html


    【解决方案1】:

    试试这个:

    localStorage.removeItem('keyName');
    localStorage.setItem('keyName','keyValue');
    

    我相信您总是必须在设置之前删除。

    【讨论】:

      【解决方案2】:

      我找到了某种答案。我想知道其他一些 Cocoa Touch 开发人员是否可以对此有所了解。我用来加载 html 的代码是这样的:

      -(void)setUpWebview
      {
          NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" 
                                                               ofType:@"html" 
                                                          inDirectory:@"/proj_folder" ];
      
          NSString *html = [NSString stringWithContentsOfFile:htmlPath 
                                                     encoding:NSUTF8StringEncoding 
                                                        error:nil];
      
          [webView loadHTMLString:html 
                          baseURL:[NSURL fileURLWithPath:
                                   [NSString stringWithFormat:@"%@/proj_folder/", 
                                    [[NSBundle mainBundle] bundlePath]]]];
      
          // disable scrolling on webview
          [[[webView subviews] lastObject] setScrollEnabled:NO];
      
      }
      

      我仅在加载的第一页(index.html)上收到“安全错误:DOM 异常 18”。一旦我移动到其他页面,这不会发生。我发现一个糟糕的解决方法是当一个人到达索引页面时重新加载。

      我确定有办法从 Obj C 端禁用此安全功能?

      谢谢。

      【讨论】:

        猜你喜欢
        • 2012-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-23
        • 1970-01-01
        • 2012-05-09
        • 1970-01-01
        • 2011-03-26
        相关资源
        最近更新 更多