Prevent Redirect Response with Alamofire in Swift

Alamofire: Follow HTTP redirects (or not)

Flexible redirect handling is now in Alamofire thanks to another pull request and is available with Alamofire 1.2.0.

Alamofire - Allow redirecting again

Figured it out:

// Restore to original behaviour
let delegate = Alamofire.SessionManager.default.delegate
delegate.taskWillPerformHTTPRedirection = nil

Thanks to https://nacho4d-nacho4d.blogspot.com/2017/08/preventing-alamofire-redirect.html

taskWillPerformHTTPRedirection never called in Alamofire 5

There are three things here:

First, session?.request(urlRequest) will never actually make a request, since you never call resume() (or attach a response handler).

Second, using a one off Session like that is not recommended. As soon as the Session goes out of scope all requests will be cancelled.

Third, EventMonitors cannot interact with the request pipeline, they're only observational. Instead, use Alamofire 5's new RedirectHandler protocol or Redirector type to handle redirects. There is more in our documentation. A simple implementation that customizes the action performed would be:

let redirector = Redirector(behavior: .modify { task, request, response in
// Customize behavior.
})
session?.request(urlRequest).redirect(using: redirector)

Switching views with Alamofire result and present()

Use the closures/callbacks, so after getting response your success and error callbacks will be called. Here is the example of that method.

class func alamofireRequest(URLString: String, parameters : Parameters, method:HTTPMethod, successCallback: @escaping (NSDictionary) -> Void, errorCallBack: @escaping (String) -> Void) -> Void {

Alamofire.request(URLString, method: method, parameters: parameters).responseJSON { response in
print(response)
if let JSON = response.result.value as? [String: Any] {
print(JSON)
successCallback(JSON as NSDictionary)
} else {
errorCallBack("JSON Doesn't Exist")
}
}

}

and called this method in your class like

 let params : [String: String] = ["sUser" : nm, "sPass" : ps, "sToken": "''"]
get(parameters: params)

restManager.alamofireRequest(URLString: url,method:.get parameters: params, successCallback: { (dict) in

// Yahoo! You get the response do whatever you want

}) { (error) in

// Show error

}

Alamofire DataRequest has no serializingDecodable

If you don't have serializingDecodable, you may not be using the required version of Xcode, 13.3+. Older versions of Xcode won't show the concurrency APIs at all.



Related Topics



Leave a reply



Submit