ios - GCDWebServer - 文档中的静态文件

标签 ios objective-c gcdwebserver

我正在使用 GCDWebServer 在我的设备上创建 HTTP 服务器。 网页的实际内容位于我的应用程序的 Documents 文件夹内。 内容包含一个带有引用的 css 和 png 文件的 HTML 文件。问题是css和png 文件无法从服务器访问/使用(我只能看到 HTML 文本内容)。

相关代码:

self.server = [[GCDWebServer alloc] init];

NSString *documents = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] absoluteString];
documents = [documents stringByAppendingPathComponent:@"MyWebsite"];

[self.server addGETHandlerForBasePath:@"/" directoryPath:documents indexFilename:nil cacheAge:0 allowRangeRequests:YES];

__weak typeof(self) weakSelf = self;

[_server addDefaultHandlerForMethod:@"GET" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest *request) {

        if ([[request path] isEqualToString:@"/status"]) {

            return [GCDWebServerDataResponse responseWithText:@"RUNNING"];
        }

        else if ([[request path] isEqualToString:@"/"]) {

            NSString *filePath = [documents stringByAppendingPathComponent:@"mypage.html"];
            NSString *html = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
            return [GCDWebServerDataResponse responseWithHTML:html];

        }    

    return nil;

}];

[self.server startWithOptions:@{GCDWebServerOption_Port: @(80)} error:&error];

谢谢!

最佳答案

我找到了解决方案:

使用 server addGETHandlerForBasePath 添加处理程序,然后使用 server addDefaultHandlerForMethod: 似乎是错误的。

我必须进行以下更改:

addGETHandlerForBasePath:

// using `absoluteString` is adding "file://" in front of the path. This seems to be wrong. The following code will create a correct path string.
const char *path = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] fileSystemRepresentation];
NSString *documents = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:path length:strlen(path)];

// full path to the HTML file
NSString *htmlFile = [documents stringByAppendingPathComponent:...];

// "directoryPath" has to be the path to folder with the (main) HTML, not the subfolder of the files. I have a separate folder only for the static files. GCDWebServer will search for the files by it's self - recursive.
[self.server addGETHandlerForBasePath:@"/" directoryPath: documents indexFilename:htmlFile cacheAge:0 allowRangeRequests:YES];

添加其他 GET 处理程序:

我必须使用 addHandler... 方法。

[self.server addHandlerForMethod:@"GET" path:@"/status" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest *request) {

    // ... same implementation as in the question
}];

关于ios - GCDWebServer - 文档中的静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30984084/

相关文章:

ios - 向自定义键盘添加建议

ios - 如何从设备获取 PDF 文件以便能够从我的应用程序上传它?

swift - 如何理解这段 GCDWebServer swift 单元测试代码?

ios - 转换 Ios 相册路径 url 与文档目录文件路径相同

ios - Swift:通过深度链接到 Twitter 应用程序将图像共享到 Twitter

ios - CollectionView 单元格中的间距

ios - 部署目标为 7.0 Objective-C 的 Xcode7.3 的缺点

iphone - 在通用应用程序中呈现 View Controller ,iOS 4 到 6

swift - 如何在GCDWebUploader中限制上传文件类型/格式

iOS Swift - 从选定的 TableView 单元格获取背景颜色