gpt4 book ai didi

html - 在不使用基本路径的情况下从 UIWebView 链接到文档中的 css 文件

转载 作者:行者123 更新时间:2023-11-29 12:33:57 24 4
gpt4 key购买 nike

我已经像这样加载了 HTML 字符串(在将 css 文件下载到文档之后):

/* the result is HTML file with references to local resources like 
<link href="file:///Users/user/Library/Developer/CoreSimulator/Devices/123/data/Containers/Data/Application/123/Documents/gen1.css" rel="stylesheet" type="text/css">

and to external resources like

<link href="http://api.tiles.mapbox.com/mapbox.js/v1.6.4/mapbox.css" rel="stylesheet" />
*/
PageGenerator *generator = [PageGenerator sharedGenerator];
NSString *result = [generator generatePageWithOptions:options];
// filesDir is file:// + [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] + /
[self.webView loadHTMLString:result baseURL:[NSURL URLWithString:[generator filesDir]]];

现在,问题是外部资源包含类似“//www.site.com/img.png”的 url,而 UIWebView 将它们重写为“file://www.site.com/img.png”,并得到 404 错误。

如何在不需要设置基本 URL 的情况下访问本地资源?或者如何将“//”重写为“http://”而不是“file://”?

非常感谢。

最佳答案

你想要做的是通过创建一个类来安装自定义 NSURLProtocol,该类是 NSURLProtocol 类型的子类并实现 NSURLConnectionDelegate 并使用 [NSURLProtocol registerClass:[MyURLProtocol class]]; 注册它>

This is a comprehensive tutorial on doing what you want to do .

任何时候你的 webview 请求一个 Assets ,无论是 css 文件还是图像,它都会调用 + (BOOL)canInitWithRequest:(NSURLRequest *)request 请求。您可以使用 [request.URL.scheme isEqualToString:@"file'] 检查 NSURLRequest 以查看 Assets 是否具有特定方案。在这种情况下,您可以返回 YES 以便您的协议(protocol)委托(delegate)可以接管请求。

然而,您会想要覆盖使用的 URL(或者至少是它发生的结果)。

我认为你应该能够使用这样的东西:

- (void)startLoading {
NSURLRequest* defaultRequest = self.request; // This is the request for file://...
NSMutableURLRequest* request = [defaultRequest mutableCopy];
// Change the URL or whatever you want on your request here.
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}

如果这不起作用,您可以随时查看更改 + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request 以返回修改后的请求。请注意,对于给定的请求,返回的结果应该始终相同,因为 iOS 缓存机制使用您返回的 NSURLRequest。

然后您可以将上面链接中显示的其余代码添加到您的委托(delegate)中:

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
return request;
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b {
return [super requestIsCacheEquivalent:a toRequest:b];
}

- (void)stopLoading {
[self.connection cancel];
self.connection = nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.client URLProtocol:self didLoadData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self.client URLProtocolDidFinishLoading:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self.client URLProtocol:self didFailWithError:error];
}

This is another example有人已覆盖图像请求以始终返回 David Hasselhoff 的照片。

这个机制非常强大。例如,您可以通过在运行时将请求的每个 css 文件替换为另一个而不更改任何 html 来使用它来为应用设置主题。

关于html - 在不使用基本路径的情况下从 UIWebView 链接到文档中的 css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26869370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com