iPhone NSURL get last redirected url

前端 未结 4 1103
轮回少年
轮回少年 2021-01-01 03:01

i wonder how can you update your NSURL to the last url that a your url will redirect to
using NSURL or NSRequest

appreciate your help. thanks.

4条回答
  •  轮回少年
    2021-01-01 03:06

    You cause following swift code to get the final redirected url:

                let request = NSMutableURLRequest(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 15.0)
            request.httpMethod = "HEAD"
            var response: URLResponse? = nil
            do {
                try NSURLConnection.sendSynchronousRequest(request as URLRequest, returning: &response)
            } catch { //Handle exception //if any
            }
            let finalURL = response?.url
    

提交回复
热议问题