iosgcdwebserver

How to set GCDWebServerOption_AutomaticallySuspendInBackground to NO


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


Solution

  • 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];