gpt4 book ai didi

swift - Alamofire 相当于 AFHTTPSessionManager

转载 作者:搜寻专家 更新时间:2023-10-31 08:08:46 51 4
gpt4 key购买 nike

对于 AFNetworking,我遵循了创建 AFHTTPSessionManager 的应用程序特定子类的建议模式。我的看起来像这样:

static NSString* Username = @"";
static NSString* Password = @"";
static NSString* BaseURL = @"https://abc.xyz.com:12345/";

@implementation HttpConnection

+ (HttpConnection*) current {
static HttpConnection* current = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
current = [[self alloc] initWithBaseURL: [NSURL URLWithString: BaseURL]];
current.securityPolicy.allowInvalidCertificates = YES;
current.responseSerializer = [AFJSONResponseSerializer serializer];
current.requestSerializer = [AFJSONRequestSerializer serializer];
});
[current.requestSerializer setAuthorizationHeaderFieldWithUsername: Username password: Password];
return current;
}

我很好奇我应该如何将此模式转换为与 Alamofire 一起使用。是不是像下面这样?

static let BaseURL = "https://abc.xyz.com:12345/"
static var User = ""
static var Password = ""

func myAppRequest((method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL) -> Request {
let request = Alamofire.request(method, BaseURL + URLString, parameters, encoding)
request.authenticate(user: User, password: "Password)
return request
}

查看 Alamofire 代码,我有一种预感,它可能会在适当的时间操作 Alamofire.Manager.sharedInstance(应用启动时的 baseURL,以及用户/密码,只要这些发生变化)。但这不太明显(如果确实可能的话)。

最佳答案

我正在使用 Alamofire.Manager 来处理所有请求。我就是这样做的。

//initialize with all details such as referrer etc
self.manager = Alamofire.Manager(configuration: cfg)


let user = "user"
let password = "password"
// you may manipulate configuration later on if you want.
self.manager.session.configuration.HTTPAdditionalHeaders!["Referer"] = self.host
self.manager.request(.GET, "https://httpbin.org/basic-auth/\(user)/\(password)")
.authenticate(user: user, password: password)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("Error: \(error)")
failure(res, json, error)
return
}
else {
// do something with JSON
}
}

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

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