如何设置 GCDWebServerOption_AutomaticallySuspendInBackground 为 NO

How to set GCDWebServerOption_AutomaticallySuspendInBackground to NO

我是 iOS 和 swift 的新手,对 android 有一些经验。我正在使用 GCDWebUploader。它工作正常。

当应用程序处于后台时,服务器暂停。我知道 iOS 后台执行中的限制。我不想改变这种行为。

但我在 GCDWebServer 文档中发现我们可以禁用此暂停。在这里查看 https://github.com/swisspol/GCDWebServer#gcdwebserver--background-mode-for-ios-apps。具体这部分

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).

这个选项怎么设置。我试过了

GCDWebServerOption_AutomaticallySuspendInBackground = "NO"

我得到了明显的错误:

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

您应该使用 NSDictionary 通过以下方法从 GCDWebServer 实例传递配置选项:

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

编辑: Objective-C 中带有 即时 字典的实际示例:

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

Swift代码

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

小提示:如果要更改GCDWebServer日志级别可以使用静态方法:

[GCDWebServer setLogLevel:4];