如何向Almofire请求添加参数 [英] How to add parameter to Almofire request
问题描述
免责声明:我是iOS编程新手,所以这个问题可能很简单。这不是一个技巧问题!
Disclaimer: I'm new to iOS programming, so this question is probably as simple as it looks. It's not a trick question!
我有一个使用Almofire发送HTTP请求的Swift项目。我想为每个请求的查询字符串添加一个参数。
I've a Swift project that uses Almofire to send HTTP requests. I want to add a parameter to the query string for every single request made.
所以,我想添加 mykey = myval
每个请求。
So, I want to add mykey=myval
to every request.
- EG:
http://example.com/index - > http://example.com/index?mykey=myval
- EG:
http://example.com/index?key = val - > http://example.com/index?key=val&mykey=myval
- EG:
http://example.com/index -> http://example.com/index?mykey=myval
- EG:
http://example.com/index?key=val -> http://example.com/index?key=val&mykey=myval
我找到了所有请求似乎都经过
I have found that all requests seem to go through
public func request(URLRequest: URLRequestConvertible) -> Request {
return Manager.sharedInstance.request(URLRequest.URLRequest)
}
在一个名为Almofire.swift的文件中
in a file named Almofire.swift
并通过
public func request(URLRequest: URLRequestConvertible) -> Request {
var dataTask: NSURLSessionDataTask?
dispatch_sync(queue) {
dataTask = self.session.dataTaskWithRequest(URLRequest.URLRequest)
}
let request = Request(session: session, task: dataTask!)
delegate[request.delegate.task] = request.delegate
if startRequestsImmediately {
request.resume()
}
return request
}
,所以我假设我需要在这里添加一些代码。由于我缺乏Swift知识,我花了几个小时进行实验但没有快乐 - 只有例外。
in a file named Manager.swift, so I'm presuming I need to add a bit of code here. Due to my lack of Swift knowledge I've spend hours experimenting but no joy - only exceptions.
有谁知道如何为所有请求添加参数?
Does anyone know how I can add a parameter to all requests?
推荐答案
您无需更改Alamofire代码中的任何内容。相反,您可以使用 URLRequestConvertible
协议将您的网址和参数封装在枚举中:
You don't need to change anything in Alamofire's code. Instead you can use the URLRequestConvertible
protocol to encapsulate your URLs and parameter in an enum:
enum Router: URLRequestConvertible {
static let baseURLString = "https://example.com" // define your base URL here
static var defaultParams = ["myKey": "myValue"] // set the default params here
// define a case for every request you need
case Index
case Endpoint1(param: String)
case Endpoint2(param1: String, param2: String)
var URLRequest: NSMutableURLRequest {
let result: (path: String, parameters: [String: AnyObject]) = {
// set the path and params for each request
switch self {
case .Index:
return ("/index", Router.defaultParams)
case .Endpoint1(let param):
var params = Router.defaultParams
params.updateValue(param, forKey: "key")
return ("/endpoint", params)
case .Endpoint2(let param1, let param2):
var params = Router.defaultParams
params.updateValue(param1, forKey: "key1")
params.updateValue(param2, forKey: "key2")
return ("/endpoint2", params)
}
}()
// create the URL and the request
let URL = NSURL(string: Router.baseURLString)!
let URLRequest = NSURLRequest(URL: URL.URLByAppendingPathComponent(result.path))
let encoding = Alamofire.ParameterEncoding.URL
return encoding.encode(URLRequest, parameters: result.parameters).0
}
}
然后你可以打电话给你的请求在以下问题中:
Then you can call your requests in the following matter:
// sends a request to 'https://example.com/index?myKey=myValue'
Alamofire.request(Router.Index).response { (request, urlResponse, data, error) -> Void in
// handle response
}
// sends a request to 'https://example.com/endpoint?key=value&myKey=myValue'
Alamofire.request(Router.Endpoint1(param: "value")).response { (request, urlResponse, data, error) -> Void in
// handle response
}
// sends a request to 'https://example.com/endpoint2?key1=value1&key2=value2&myKey=myValue'
Alamofire.request(Router.Endpoint2(param1: "value1", param2: "value2")).response { (request, urlResponse, data, error) -> Void in
// handle response
}
这篇关于如何向Almofire请求添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!