I'm using GCDWebServer to create a HTTP server on my device. The actual content for the webpage is placed inside the Documents folder of the my App. The content contains a HTML file with a referred css and png file. The problem is that the css and png files are not accessible/usable from the server (I can only see the HTML text content).
The relevant code:
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];
Thanks!
I found a solution:
Adding the handler with
server addGETHandlerForBasePath
and thenserver addDefaultHandlerForMethod:
seems to be wrong.I had to do the following changes:
addGETHandlerForBasePath:
add other GET handler:
I have to use the
addHandler...
method.