I´ve read the question about how to open a mobileconfig file stored in the app (Unable to open mobileconfig file in Safari ios devices[resolved]) I need to do the same in my app but i dont know how to make a server running in background I´ve tried with CocoaHTTPServer and GCDWebServer but in both I need wifi connection to make the server. I need to do a local server to simulate a URL without network to lauch the .mobileconfig If anyone can help me I´ll be very grateful. Thanks!
-
[Discussion in this question about programmatically installing configuration file](http://stackoverflow.com/questions/2338035/installing-a-configuration-profile-on-iphone-programmatically) has comments as late as March 2014 which clearly state that what you are trying to achieve is not possible... However, i wonder how the new Testflight 'app' by apple does profile validation ... – Swapnil Luktuke May 04 '15 at 11:31
3 Answers
You can run a server while your app is in the background, but only for a few minutes (and iOS can still freeze your app at any time).
In the case of GCDWebServer, set the GCDWebServerOption_AutomaticallySuspendInBackground
option to NO
and make sure to start a background task when your app is sent to the background. This will keep your app alive and GCDWebServer won't automatically suspend itself either.
If your app gets frozen while in background, be sure to stop GCDWebServer and restart it when your app comes back to the foreground.

- 3,848
- 1
- 38
- 55
-
very late here and new to this. How are you setting the value of GCDWebServerOption_AutomaticallySuspendInBackground. I am getting an error ": Cannot assign to value: 'GCDWebServerOption_AutomaticallySuspendInBackground' is a 'let' constant". – lvin Jun 29 '16 at 10:43
-
I also added this as a question [here](http://stackoverflow.com/questions/38101065/how-to-set-gcdwebserveroption-automaticallysuspendinbackground-to-no). Would be great if you could check it out. Thank you! – lvin Jun 29 '16 at 13:31
-
@Pol The GCD readme states that it already has a background task running, and with the GCDWebServerOption_AutomaticallySuspendInBackground: NO set, it will stay alive. That said, my instance freezes, or goes down, after just a couple of seconds in background. Any thoughts on this? – DwarDoh Aug 23 '17 at 19:45
Swift 4.0/Swift 5.3 Answer
Replace your current server initialisation_code which should be like below
webServer.start(withPort: 8080, bonjourName: "GCD Web Server")
With the code below
let options:[String : Any] = ["Port" : 8080, "AutomaticallySuspendInBackground" : false]
try! webServer.start(options: options)
It just initialises the server with different method.
Description :
- It uses
webServer.start(options: [String : Any])
property instead ofwebServer.start()
property which is used for to initialise the server. - As @Pol said here and on GitHub, it does the same thing (setting
GCDWebServerOption_AutomaticallySuspendInBackground
option toNO
) - Also make sure that in your
info.plist
, you haven't setApplication does not run in background
valueYES
(it will also work if you haven't assigned any property like that ininfo.plist
)

- 1,078
- 1
- 8
- 30
Presumably you want to do this so that websites in Safari or WebKit browsers like Chrome/Firefox would access information hosted by your server running on iOS.
While Apple Review won't let you just run a process like that in the background for more than a few minutes, starting in iOS 15 you will be able to achieve what you want!
Finally, Safari Web Extensions are coming out. In true Apple fashion, they will only contain a subset of the total Web Extensions standard, but you'll be able to port quite a bit to them. They also contain a way to send messages to your app from Javascript, and vice versa. Also known as Requests ;-)
So you can basically have the user install your app and Safari Web Extension, and then your extension can communicate with your app extension :)
Another way to accomplish something similar is App Clips in Safari. They will display a banner for your app, which can download and open some interface, without being installed from the app store. It's a more clunky experience, but it's available for when your app isn't even installed!

- 1,883
- 2
- 25
- 35