2015-10-09 105 views
0

免責聲明:我是iOS新手,因此這個問題可能看起來很簡單。這不是一個狡猾的問題!如何向Almofire請求添加參數

我有一個使用Almofire發送HTTP請求的Swift項目。我想爲查詢字符串中的每個請求添加一個參數。

因此,我想爲每個請求添加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

我發現,所有的請求似乎在一個名爲Almofire.swift

和文件要經過

public func request(URLRequest: URLRequestConvertible) -> Request { 
    return Manager.sharedInstance.request(URLRequest.URLRequest) 
} 

也通過

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 
} 

在一個名爲Manager.swift的文件中,所以我假設我需要在這裏添加一些代碼。由於我缺乏斯威夫特的知識,我花了數小時試驗,但沒有喜悅 - 只有例外。

有誰知道我可以如何向所有請求添加參數?

回答

2

您無需更改Alamofire代碼中的任何內容。相反,你可以使用URLRequestConvertible協議封裝的URL和參數的枚舉:

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 
    } 
} 

然後,您可以撥打以下不管你的請求:

// 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 
} 
+0

謝謝你。我知道如果文件被覆蓋,我將會丟失我的更改。我可以在Almofire上創建一個具有相同方法簽名的包裝器,並將其更改放在那裏。但是我仍然需要知道如何在創建請求後添加到請求中。 –

+0

請參閱我更新的答案,以解決您的問題。 – joern

0

簡單的請求 - >

func someFunction() 
{ 
    Alamofire.request(.GET, "apiName", parameters:["Key":"Value"]) 
     .response { request, response, data, error in 
      if error == nil { 
       print(request) 
       print(response) 
      } 
      else { 
       //Display Error Message 
       print(error) 
      } 

    } 
}