How to set GCDWebServerOption_AutomaticallySuspendInBackground to NO

一笑奈何 提交于 2019-12-10 11:54:57

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/38101065/how-to-set-gcdwebserveroption-automaticallysuspendinbackground-to-no

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!