本文介绍了在Mac OS X上使用Cocoa WebView的合成Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Webview,我可以完全控制它加载的内容。基本上我想提供一个类的实例,它扮演URL中命名的各种Web服务器的角色。理想情况下,我将为这个webview提供一个委托,它会告诉委托人它需要的每个URL,委托人将回应MIME类型和数据。

I would like to create a Webview where I have total control over the content it loads. Essentially I want to provide an instance of a class that plays the role of the various web servers named in the URLs. Ideally, I would provide this webview with a delegate and it would tell the delegate each URL it requires and the delegate would respond with the MIME type and data.

这将是一个web服务器运行在一些自定义端口(说2222),并提供一个初始URL,如。但是,这只允许我拦截同一主机上的相对URL,而不是截取到其他主机的绝对URL。

One way to do this would be to literally have a web server running on some custom port (say 2222) and supply an initial URL like http://localhost:2222/initial/base/url. However, this only allows me to intercept relative URLs on the same host, not absolute URLs to other hosts.

我在文档中看到的WebView代理似乎允许用户高级控制,例如选择要加载的网址的策略,或监控加载进度,但不能拦截和控制自身加载的数据。

The WebView delegates I saw in the documentation seems to allow the user high-level control, such as choosing a policy of which URLs to load, or monitoring the progress of loads, but not the ability to intercept and control the data which is loaded itself.

任何有关我如何实现这一目标的提示?

Any hints on how I might achieve this goal?

推荐答案

服务此函数。你注册你的类作为协议(也许更好地称为协议处理程序),每个NSURLRequest将被路由到你,询问你是否可以处理它。您说YES,然后您的对象可以完全控制URL加载进程。这涉及到主要的URL加载引擎,所以它负责所有的WebViews,NSURLConnections等等。参见一些更多的指针。

NSURLProtocol serves this function. You register your class as a protocol (maybe better called a protocol handler), and every NSURLRequest will be routed to you to ask whether you can handle it. You say YES and then your object has full control over the URL loading process. This ties into the main URL loading engine, so it takes care of all WebViews, NSURLConnections, etc. See this question for some more pointers.

这篇关于在Mac OS X上使用Cocoa WebView的合成Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 06:11