gpt4 book ai didi

ios - AFHTTPSessionManager 子类 swift

转载 作者:行者123 更新时间:2023-11-28 06:55:08 30 4
gpt4 key购买 nike

使用自定义 session 配置创建 AFHTTPSessionManager 单例子类的正确方法是什么?

class DefaultSessionConfiguration: NSURLSessionConfiguration {

override init () {

super.init()

self.HTTPShouldSetCookies = true
HTTPCookieStorage?.cookieAcceptPolicy = NSHTTPCookieAcceptPolicy.Always
HTTPAdditionalHeaders = [ "Content-Type": "application/json"];
}

}

let baseUrl = "https://google.com"

class HTTPManager: AFHTTPSessionManager {


static let _sharedAPIManager = HTTPManager(baseURL: NSURL(string: baseUrl)!, sessionConfiguration:DefaultSessionConfiguration())

class var sharedInstance : HTTPManager {
return _sharedAPIManager
}

override init(baseURL url: NSURL!, sessionConfiguration session:NSURLSessionConfiguration? ) {

super.init(baseURL: url, sessionConfiguration: session)
self.responseSerializer = AFJSONResponseSerializer() as AFJSONResponseSerializer
self.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer

}

}

当尝试将此代码用作 self.sessionManager = HTTPManager.sharedInstance 时,它总是崩溃并显示消息

[MyApp.DefaultSessionConfiguration setHTTPShouldSetCookies:]: unrecognized selector sent to instance 0x7f99f8f31b70

但 MyApp.DefaultSessionConfiguration 是 NSURLSessionConfiguration 的子类,并且 defenetly 具有此方法。

那么,我们如何解决这个问题呢?

最佳答案

问题是您不应该直接实例化 NSURLSessionConfiguration。您应该通过调用以下类方法之一来实例化它:defaultSessionConfigurationephemeralSessionConfigurationbackgroundSessionConfigurationWithIdentifier。例如:

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPShouldSetCookies = true
configuration.HTTPCookieStorage?.cookieAcceptPolicy = .Always

关于ios - AFHTTPSessionManager 子类 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33853319/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com