I use Alamofire to make all network API calls to get a bunch of different JSON files (depending on what mode the user is in).
Some of JSON receive user profile data, some user settings, some data for viewing the table.
I use below
let postParameters: [String: AnyObject] = ["apiKey":"\(Constants.Variables.apiKey)",
"domain":"\(Constants.Variables.apiDomain)",
"authToken":"\(authToken)",
"password":"\(password)",
"from":"\(from)",
"to":"\(to)"]
Alamofire.request(Router.GetActivities(postParameters))
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
if response.result.isSuccess {
let responseJSON = JSON(response.result.value!)
}
}
and I have a router installed
enum Router: URLRequestConvertible {
static let baseURLString = "\(Constants.APIPath.URL)"
static var OAuthToken: String?
case GetActivities([String: AnyObject])
var method: Alamofire.Method {
switch self {
case .GetActivities:
return .GET
}
}
var path: String {
switch self {
case .GetActivities(_):
return "/activity.php/get"
}
}
var URLRequest: NSMutableURLRequest {
let URL = NSURL(string: Router.baseURLString)!
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
mutableURLRequest.HTTPMethod = method.rawValue
if let token = Router.OAuthToken {
mutableURLRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
switch self {
case .GetActivities(let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
}
}
}
Each time I load any view that has a table view (e.g. view2), the application retrieves the data from the API. The table view is part of the navigation controller, and the transition to the view re-requests the data, but does not return to the view.
Example:
- Case 1: view1 -> view2 = re-fetch JSON each time independently
- 2: view2 → view3 = ,
- 3: view3 → view2 = JSON
- 4: view2 → view1 = , view2 , .
, view1 → view2 , , - , , .
Swift, JSON Alamofire Haneke .
Haneke , , , Alamofires.
.