Html 在不中断链接的情况下将本地文件注入UIWebView

Html 在不中断链接的情况下将本地文件注入UIWebView,html,xcode,uiwebview,local,inject,Html,Xcode,Uiwebview,Local,Inject,我正在开发一个iOS应用程序,它需要在UIWebView中显示服务器上的网页,同时根据需要注入相关的本地png和css文件,以加快加载时间。以下是我尝试执行此操作的代码: NSData *myFileData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.example.com/index.html"]]]; NSString* myFile

我正在开发一个iOS应用程序,它需要在UIWebView中显示服务器上的网页,同时根据需要注入相关的本地png和css文件,以加快加载时间。以下是我尝试执行此操作的代码:

NSData *myFileData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.example.com/index.html"]]];
NSString* myFileHtml = [[NSString alloc] initWithData:myFileData encoding:NSASCIIStringEncoding];
[myWebView loadHTMLString:myFileHtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
我的问题是,一些网页中有链接到服务器上其他网页的按钮,并且由于UIWebView仅加载字符串,点击按钮时不会导致UIWebView加载新网页URL,就像我使用loadRequest方法一样

我的问题是,如何让UIWebView在仍然从baseurl注入本地文件的情况下表现得像加载请求一样


谢谢

NSURLPRotocol是NSURLConnection的处理程序,它将使您有机会拦截对服务器的调用并替换您自己的内容

1) 从NSURlProtocol派生一个类

2) 在应用程序中调用NSURLProtocol注册表类:didFinishLaunchingWithOption

3) 阅读有关在必要时实施这些方法的文档: initWithRequest:cachedResponse:client:, 惊人的, URLProtocol:didReceiverResponse:cacheStoragePolicy:
URLProtocoldFinishLoading:

NSURLPRotocol是NSURLConnection的处理程序,它将使您有机会拦截对服务器的调用并替换您自己的内容

1) 从NSURlProtocol派生一个类

2) 在应用程序中调用NSURLProtocol注册表类:didFinishLaunchingWithOption

3) 阅读有关在必要时实施这些方法的文档: initWithRequest:cachedResponse:client:, 惊人的, URLProtocol:didReceiverResponse:cacheStoragePolicy:
URLProtocoldFinishLoading:

按钮中的相对链接无法工作,因为链接的页面位于远程服务器上,而不在设备的文件系统上。但是,您可以使用UIWebViewDelegate方法使其工作:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSString *localRootPath = [NSString stringWithFormat:@"file://%@", [[NSBundle mainBundle] bundlePath]];
        NSString *remoteRootPath = @"http://yourdomain.com";

        NSString *remotePath = [[request.URL absoluteString] stringByReplacingOccurrencesOfString:localRootPath withString:remoteRootPath]; 

        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:remotePath]]];

        // or you can use your own loading mechanism here

        return NO;
    }

    return YES;
}

此方法拦截来自WebView的所有请求。如果请求是由用户点击/单击触发的,则URL将从相对URL修改为绝对URL,以便可以从服务器加载。不要忘记在WebView上设置委托,否则将不会调用此方法。

按钮中的相对链接无法工作,因为链接的页面位于远程服务器上,而不在设备的文件系统上。但是,您可以使用UIWebViewDelegate方法使其工作:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSString *localRootPath = [NSString stringWithFormat:@"file://%@", [[NSBundle mainBundle] bundlePath]];
        NSString *remoteRootPath = @"http://yourdomain.com";

        NSString *remotePath = [[request.URL absoluteString] stringByReplacingOccurrencesOfString:localRootPath withString:remoteRootPath]; 

        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:remotePath]]];

        // or you can use your own loading mechanism here

        return NO;
    }

    return YES;
}

此方法拦截来自WebView的所有请求。如果请求是由用户点击/单击触发的,则URL将从相对URL修改为绝对URL,以便可以从服务器加载。不要忘记在WebView上设置委托,否则将不会调用此方法。

按钮链接是相对链接还是绝对链接?它们在html中显示为,所以相对按钮链接是相对链接还是绝对链接?它们在html中显示为,所以相对