TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Nov 2016 |
SwiftSwift Version | 3 |
SPMSupports SPM | ✗ |
Maintained by Alex Corlatti.
Depends on: | |
Alamofire | ~> 4 |
SwiftyJSON | ~> 3 |
AlamofireRouter is a simple Alamofire router. If you need swift 2.3 version, use the 0.1.0 pod version
To run the example project, clone the repo, and run pod install
from the Example directory first.
In your viewcontroller create the shared instance of the AlamofireUIManager
like in this way
class MyViewController: UIViewController {
let netManager = AlamofireUIManager.sharedInstance
...
In viewDidLoad()
method set the delegate
override func viewDidLoad() {
super.viewDidLoad()
netManager.delegate = self
...
Implements the delegate methods:
createSpinner() -> UIView
customize the view during loading datacloseSpinner(spinner: UIView?)
for removing your customized viewcheckJson(json: JSON, showError: Bool, completionHandler: AFRequestCompletionHandler, errorHandler: AFRequestErrorHandler) {
for cheching the JSON response before passing it to the completition handlermanageAlertError(error: NSError?, completition: @escaping AFRequestCompletionVoid)
customize the alert viewFor example:
extension MyViewController: AlamofireUIManagerDelegate {
func createSpinner() -> UIView {
let act = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
act.center = self.view.center
act.activityIndicatorViewStyle = .Gray
self.view.addSubview(act)
act.startAnimating()
return act
}
func closeSpinner(spinner: UIView?) {
guard spinner != nil else { return }
if let act = spinner as? UIActivityIndicatorView {
act.stopAnimating()
act.removeFromSuperview()
}
}
func checkJson(json: JSON, showError: Bool, completionHandler: AFRequestCompletionHandler, errorHandler: AFRequestErrorHandler) {
if let errorStr = json["error"]["message"].string { // Probably authorization required
let error = NSError(domain: "json", code: 401, userInfo: ["info": errorStr])
errorHandler(error)
} else { completionHandler(json) }
}
func manageAlertError(error: NSError?, completition: @escaping AFRequestCompletionVoid) {
let alertController = UIAlertController(title: "Error", message: error?.description, preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: { _ in
self.netManager.closeAlert()
completition()
})
alertController.addAction(defaultAction)
presentViewController(alertController, animated: true, completion: nil)
}
}
Alex Corlatti, [email protected]
AlamofireUIManager is available under the MIT license. See the LICENSE file for more info.