0

I am new to iOS and swift with some experience in android. I am using the GCDWebUploader. Its working fine.

The server suspends when the app is in background. I am aware of the constraints in iOS Background Execution. I dont want to change that behaviour.

But I found in GCDWebServer documentation that we can disable this suspension. Check here https://github.com/swisspol/GCDWebServer#gcdwebserver--background-mode-for-ios-apps. Specifically this part

If the app goes in the background while no HTTP connections are opened, GCDWebServer will immediately suspend itself and stop accepting new connections as if you had called -stop (this behavior can be disabled with the ****GCDWebServerOption_AutomaticallySuspendInBackground**** option).

How do you set this option. I tried

GCDWebServerOption_AutomaticallySuspendInBackground = "NO"

And I get the obvious error:

Cannot assign to value: 'GCDWebServerOption_AutomaticallySuspendInBackground' is a 'let' constant

lvin
  • 290
  • 3
  • 11

1 Answers1

4

You are supposed to pass configuration options using a NSDictionary with the following method from a GCDWebServer instance:

- (BOOL)startWithOptions:(NSDictionary*)options error:(NSError**)error;

Edit: a practical example with an on-the-fly dictionary in Objective-C:

NSError*myError = nil;
self.webServer = [[GCDWebServer alloc] init];
BOOL success = [self.webServer startWithOptions:@{
               GCDWebServerOption_AutomaticallySuspendInBackground : @(NO)
               } error:&myError];

Swift code

var myError: NSError?
let webServer = GCDWebServer()
webServer.startWithOptions([GCDWebServerOption_AutomaticallySuspendInBackground : false], error: myError)

A little tip: if you want to change the GCDWebServer log level you can use the static method:

[GCDWebServer setLogLevel:4];
Lookaji
  • 1,023
  • 10
  • 21
  • yes I was able to set the variable this way. But I am not seeing any change in the server suspension. It still does suspend when in background. I am using the webuploader - it wont work that way? Will be doing more reading on it. I am adding this as the answer meanwhile. Thank you! – lvin Jun 30 '16 at 18:53
  • Once I got the it working with iOS 7 by disabling the system idle timer: `[UIApplication sharedApplication].idleTimerDisabled = YES;`. I'm not quite sure, though, if it works correctly with newer iOSes since they introduced a better energy control. – Lookaji Jul 01 '16 at 08:20