Create an iOS App that runs as Web Server in background

8.8k views Asked by At

I want to make an iOS application (for iPad specifically), which behave like a web server. I saw the examples coming with COCOAHTTPSERVER, but i didn't understand something.

Is it possible for my application, while being in background, to receive a request from another application running in the same device?

If it is possible how can i do this?

Otherwise, if it is impossible, what is the meaning of a web server application, that can't work in background?

My exact problem is as follows: I need an iOS application that can receive a request from a javascript application, running on safari, in the same device. Can i create an application behaving like a web server in background (because safari must be in foreground), or there is another way to implement this?

3

There are 3 answers

1
Eric On

No, iOS does not support web servers running in the background.

The reason Apple does this is that a web server has to listen for inbound network requests. Listening for inbound network requests requires that the radios be active all the time. Active radios drain the battery.

0
casey On

This library may make your task much easier.

https://github.com/swisspol/GCDWebServer

1
Pol On

You can run a web server in the background on iOS but unfortunately only for a limited time (up to 10 minutes max) and the OS can stop it at any time. See this tech note [1] for more info.

If that limitation is acceptable to you, you should use an already existing web server like GCDWebServer which handles background mode for you [2].

[1] https://developer.apple.com/library/ios/technotes/tn2277/_index.html

[2] https://github.com/swisspol/GCDWebServer#gcdwebserver--background-mode-for-ios-apps