显示从 Drupal 站点获取并存储在 iPhone 本地的内容时出现问题
我正在与 Drupal 开发人员合作开发一个 iPhone 应用程序,该应用程序从 Drupal 站点获取部分内容,将其存储在文件系统中,并在本地 UIWebView 中显示。不过,我们遇到了一些问题,我们正在寻找解决方案,无论是在 Drupal 中还是在 iPhone 端。
设置
- 当 Drupal 内容发生更改时,它将创建 iPhone 可以下载的更改内容的清单。清单只是列出了要下载的新内容的相对链接。
- 下载后,应用程序会将内容存储在本地网页中。
- iPhone将内容加载到UIWebView中;链接在所有不同的页面之间导航。
问题
- Drupals 相对链接是相对于站点的根目录的 - 即它们包含一个前导“/”。显然,当我加载页面时,相对于文件系统根目录的链接将不起作用,因为内容驻留在应用程序的文档目录中。我可以使用
-webView:shouldStartLoadWithRequest:navigationType:
在文档目录前面添加用户点击的链接,检查 NSURLRequest 是否包含以文档目录开头的 url,如果不包含则重定向它。但是,这对页面资源(例如 CSS 文件和图像)没有帮助。 - Drupal 不会将文件扩展名附加到路径中,这会造成类似的困境。 UIWebView 的
-loadRequest
方法似乎只有在提供包含带有文件扩展名的文件的 URL 的 NSURLRequest 时才起作用。否则,我会收到 WebKitErrorDomain 错误代码 102:“帧加载中断”。或许可以使用 UIWebView 的-loadHTMLString:baseURL:
找到解决方案,但是当用户点击链接时,该方法对代码非常不友好(我现在不确定为什么) 。
忽略 -release
消息,我用来加载页面的代码是:
NSString *fileToLoad = [appDelegate.webContentDirectory stringByAppendingPathComponent:filename];
NSURL *url = [[NSURL alloc] initFileURLWithPath:fileToLoad isDirectory:NO];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:(NSTimeInterval)5];
[self.webView loadRequest:newURLRequest];
我对创造性思维持开放态度 - 例如,是否有一个我可以在应用程序内设置的 iPhone Web 服务器,它会自动处理这两个问题问题?
I am working with a Drupal developer to develope an iPhone app that fetches a portion of it's content from a Drupal site, stores it in a file system, and displays it locally in a UIWebView. We've run into a bit of a problem though, and we're looking for a solution, either in Drupal or on the iPhone end of things.
The Setup
- As Drupal content changes, it will create manifests of the changed content that the iPhone can download. The manifests simply list relative links to the new content to download.
- Once downloaded, the app stores the content locally in a web page.
- The iPhone loads the content into a UIWebView; links navigate between all the various pages.
The Problem
- Drupals relative links are relative to the root of the site - i.e. they contain a leading '/'. Obviously when I'm loading pages, links relative to the root of the filesystem won't work, since the content resides in the app's document directory. I can prepend the document directory to links the user taps using
-webView:shouldStartLoadWithRequest:navigationType:
, checking to see if the NSURLRequest contains a url starting with the document directory and redirecting it if it does not. However, this does not help with page resources - e.g. CSS files and images. - Drupal does not append file extensions to paths, which creates a similar dilemma. UIWebView's
-loadRequest
method only seems to work if it is fed an NSURLRequest that contains a URL to a file with a file extension. Otherwise I receive a WebKitErrorDomain error code 102: "Frame load interrupted". It may be possible to work out a solution using UIWebView's-loadHTMLString:baseURL:
, but that method is very unfriendly to the code when the user taps on links (I'm not sure why at this point).
Disregarding -release
messages, the code I am using to load pages is:
NSString *fileToLoad = [appDelegate.webContentDirectory stringByAppendingPathComponent:filename];
NSURL *url = [[NSURL alloc] initFileURLWithPath:fileToLoad isDirectory:NO];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:(NSTimeInterval)5];
[self.webView loadRequest:newURLRequest];
I am open to creative thinking - e.g. is there an iPhone web server I could set up in-app that would automatically take care of both problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要解决 CSS/Image 问题:当您下载清单中引用的文件时,您可以将文件内容读入
NSString
中,将文档目录路径附加到 css/image 文件的路径中,然后只需保存该字符串并覆盖“旧”文件即可。To solve the CSS/Image issue: You could read in the files content into a
NSString
when you are downloading the files refrenced from your manifest, append your document directory path to the path of your css/image files, then just save that string overwriting the "old" file.抱歉,如果我不完全理解你想做什么。但根据我的理解,您是在本地缓存节点页面的 HTML 源代码,将所有资源(图像、CSS、JavaScript 等)保留在服务器端。
为什么不使用 UIWebView 的方法:
其中参数为:
源
。
然后,UIWebView 应该无缝加载任何资源,没有任何问题
Sorry if I don't fully understand what you want to do. But from my understanding you are locally caching the HTML source of a node page, keeping all resources (images, CSS, JavaScript, etc.) on the server side.
Why don't you use UIWebView's method:
Where the parameters are:
source
of the web site
The UIWebView should then seamlessly load any resource without any problem.
子类化 NSURLProtocol 正是我们正在寻找的答案。在系统中注册我们的子类并将其配置为有选择地处理 html 请求(有效地覆盖我们认为合适的默认 html 协议处理程序)后,我能够为每个请求加载我想要的任何内容。本质上,我将 http 请求从缓存内容重定向到加载文件。
对于任何感兴趣的人,答案来自这个非常有趣的问题: 允许 UIWebView 在没有 Internet 连接的情况下加载 http://localhost:port/path URI
Subclassing NSURLProtocol is exactly the answer we were looking for. After registering our subclass with the system and configuring it to selectively handle html requests (effectively overriding the default html protocol handler where we saw fit), I was able to load whatever content I wanted to for each request. In essence I redirected http requests to the load files from the cached content.
For any who are interested, the answer was from this very interesting question: Allow UIWebView to load http://localhost:port/path URIs without an Internet connection