6

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?

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • I've found similar question on [StackOverffow][1] [1]: http://stackoverflow.com/questions/9613357/ios-background-application-network-access – Ievgenii Dec 20 '13 at 07:27

3 Answers3

6

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

Pol
  • 3,848
  • 1
  • 38
  • 55
3

This library may make your task much easier.

https://github.com/swisspol/GCDWebServer

casey
  • 1,118
  • 1
  • 13
  • 25
-3

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.

Eric
  • 2,045
  • 17
  • 24
  • I successfully run my service on background for iOS 12+ and previous, its posible. But for new iOS Apple make deprecated key part of api and recomend use BGTaskSheduler for that. I hope there is way to work for iOS 13, but at the moment not found a solution yet. – Anonimys Apr 04 '20 at 07:58