【发布时间】:2023-03-31 05:55:01
【问题描述】:
我正在开发一个使用 UIWebView 来显示基于 HTML5 的页面的 iphone 应用程序。要求包括:
1:页面需要渲染内嵌 SVG。
2:页面需要访问localStorage。
为了获得正确的 mime 类型以便内联 SVG 可以工作,我首先尝试使用下面的代码来填充 web 视图:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSURL *baseURL = [[NSURL alloc] initFileURLWithPath:resourcePath];
[self.webView loadData:htmlData
MIMEType:@"application/xhtml+xml"
textEncodingName:@"UTF-8"
baseURL:baseURL];
但是,在尝试这种技术时,我在尝试访问本地存储时不断引发 SECURITY_ERR 异常: W3C documentation on localStorage and SECURITY_ERR
我发现有人遇到同样的问题,他们认为这是由于域起源: Post covering issues with UIWebView and localStorage access issues
他们改用 NSURLRequest 解决了他们的问题:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:[path stringByAppendingString:@"path and file name of the file"]];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
[webView loadRequest:request];
这似乎解决了我的 localStorage 问题,但这会破坏我的 SVG,因为我不知道在这种情况下如何显式设置 mime 类型。
所以,问题是,我如何为内联 svg 加载具有正确 mime 类型的数据,同时仍然保持文档的来源符合 localStorage 要求?
感谢您的帮助:)
【问题讨论】:
标签: iphone objective-c svg local-storage