Prevent redirect response with Alamofire in Swift

↘锁芯ラ 提交于 2019-12-03 21:48:19

Alternative (code snippet) solution using AlamoFire 2.4 (Xcode7). In my case, I always expect a redirect. (I am unpacking a shortened link.) If the completion in the request.response call runs, that is an error to me.

func printRedirectUrl() {

    // taskWillPerformHTTPRedirectionWithCompletion: ((NSURLSession, NSURLSessionTask, NSHTTPURLResponse, NSURLRequest, NSURLRequest? -> Void) -> Void)?
    Alamofire.Manager.sharedInstance.delegate.taskWillPerformHTTPRedirectionWithCompletion = { session, task, response, request, completion in
        // request.URL has the redirected URL inside of it, no need to parse the body
        print("REDIRECT Request: \(request)")
        if let url = request.URL {
            print("Extracted URL: \(url)")
        }
        Alamofire.Manager.sharedInstance.delegate.taskWillPerformHTTPRedirection = nil // Restore redirect abilities
        return
    }

    // We expect a redirect, so the completion of this call should never execute
    let url = NSURL(string: "https://google.com")
    let request = Alamofire.request(.GET, url!)
    request.response { request, response, data, error in
        print("Logic Error, response should NOT have been called for request: \(request)")
        Alamofire.Manager.sharedInstance.delegate.taskWillPerformHTTPRedirection = nil // Restore redirect abilities - just in case
    }
}

REDIRECT Request: { URL: https://www.google.com/ }

Extracted URL: https://www.google.com/

It looks like returning nil can possibly cause a deadlock. Instead, try to create a new NSURLRequest with the same original URL. See @jhersh's notes in a previous Alamofire PR along with the comments and implementation in his tests.

How to Stop a Redirect

func disallowRedirect() {
    let URL = "http://google.com/"
    let delegate = Alamofire.Manager.sharedInstance.delegate

    delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
        return NSURLRequest(URL: NSURL(string: URL)!)
    }

    let request = Alamofire.request(.GET, URL)
    request.response { request, response, data, error in
        println("Request: \(request)")
        println("Response: \(response)")
        println("Data: \(NSString(data: data as! NSData, encoding: NSUTF8StringEncoding))")
        println("Error: \(error)")
    }
}

disallowRedirect()

The fact that you cannot pass nil into the NSURLSessionTaskDelegate method's completionHandler looks like a bug. I'm going to file a radar for this and I'll post a link to the bug report once I'm finished.

In Swift 4,

let delegate = Alamofire.SessionManager.default.delegate

        delegate.taskWillPerformHTTPRedirection = { (session, task, response, request) -> URLRequest? in
           // print("REDIRECT Request: \(request)")
            return nil
        }

I don't know if your version of Alamofire has a support for public delegate. Last time I checked delegate was private. I am using the changes made by @jhersh. You can check his additions and how to use delegate by followin github pr. https://github.com/Alamofire/Alamofire/issues/314

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!