I've never seen a simpler IOS HTTP server than Cclhttpserver. You don't even have to create any subclasses to use it. For most tasks, this small but powerful server can meet your needs.
It's simple enough to be outrageous. So I think it's enough to look at its homepage introduction:
Https://github.com/cocodelabs/CCLHTTPServer
Cclhttpserver is a simple HTTP server framework under a IOS/OSX.
I. Methods of Use
The following code creates a simple HTTP server that listens on a specified port. and responds with the specified Cclhttpserverresponse class (subclass).
Objective-c:
Cclhttpserver*server = [[Cclhttpserver alloc] Initwithinterface:nil port:8080handler:^id<cclhttpserverresponse > (id<cclhttpserverrequest>request) {
Nsdictionary *headers = @{
@ "Content-type": @ "Text/plain; Charset=utf8 ",
};
NSData *body = [@ "Hello World" datausingencoding:nsutf8stringencoding];
return [[Cclhttpserverresponse alloc]initwithstatuscode:200 headers:headers body:body];
}];
SWITF:
Varserver:cclhttpserver=cclhttpserver (Interface:nil, port:7433, Handler:
{Request in
Let headers =["Content-type": "Text/plain; Charset=utf8 "]
Let BODY = "Hello World". datausingencoding (nsutf8stringencoding);
Returncclhttpserverresponse (statuscode:200, Headers:headers, Body:body)
})
The cclhttpserverresponse must handle the request parameters passed to it. The request parameter is an object that implements the Cclhttpserverrequest protocol, which encapsulates a variety of practical methods that you can use to create a variety of response (which are described below).
The purpose of Cclhttpserver is to expose a standard o-c interface for creating HTTP servers, see the Cllhttpserver/interface folder.
Other words. These interfaces can also be used by other HTTP servers. Developers can use this interface to write their own framework without having to control the specific implementation of a server.
Ii. Request for requests
Request is an object that implements the Cclhttpserverrequest protocol. The protocol exposes the basic specifics of the HTTP request, such as the request method, path, and HTTP version number. Headers and the text.
@protocol cclhttpserverrequest<nsobject>
-(NSString *) method;
-(NSString *) path;
-(NSString *) httpversion;
-(Nsdictionary *) headers;
-(NSData *) body;
@end
Third, response responses
You can create a new response class that can also be used regardless of what has already implemented the Cclhttpserverresponse protocol. Of course, in most cases, the use of cclhttpserverresponse is sufficient.
1. Return NSData Data
[cclhttpserverresponseresponsewithstatuscode:204 Headers:nil Body:nil];
2. Return text data
[cclhttpserverresponseresponsewithstatuscode:200
Headers:nil
content:@ "Hello World"
contenttype:@ "Plain/text"];
3. Return URL-encoded name value pairs
This form is like: Name=kyle&sex=female
[cclhttpserverresponseformurlencodedresponsewithstatuscode:200
Headers:nil
parameters:@{@ "name": @ "Kyle"}];
4. Return JSON data
[cclhttpserverresponsejsonresponsewithstatuscode:200
Headers:nil
parameters:@{@ "name": @ "Kyle"}];
Four, installation
Pod ' Cclhttpserver '
"iOS Open Source Code" (1): Cclhttpserver