1

我有一个带有 webview 的 Swift 项目,它从本地文件夹 vai GCDwebserver 提供文件,但是该网站被设计为单页应用程序,有什么方法可以将所有 url 请求重写为 index.html?

我目前正在使用 GCDwebserver,但我愿意更改服务器以将 SPA 加载到 webview 中。

使用 nginx 我会做类似的事情

server {
     listen 8080;
     listen [::] 8080;
     root /root/bundled;
 location / {
      try_files $uri $uri/ /index.html;
 }
}

如何在 osx 中的 webview 上获得相同的重写效果?

4

2 回答 2

1

只需使用默认处理程序来捕获所有 GET 请求:

- (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block;

或者,如果您想做更多花哨的匹配,请使用正则表达式:

- (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block

甚至一直到匹配块:

- (void)addHandlerWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock;
于 2017-06-28T19:39:22.923 回答
0

我必须承认我从未使用过 GCDWebserver,但如果您在 iOS 应用程序中有任何要求重定向 url,您可以使用 NSURLProtocol。你可以在这里阅读更多关于它的信息https://www.raywenderlich.com/76735/using-nsurlprotocol-swift。阅读完链接后,您基本上需要使用 canonicalRequestForRequest 方法来捕获当前请求并返回一个新请求。希望有帮助。

于 2017-06-01T07:44:18.027 回答