我就这个问题联系了 Apple 开发人员支持,他们告诉我问题是我的一个 url 重定向到另一个 url,这当然也必须在我的 plist 中列为例外。您可以使用 url 会话委托方法轻松确定重定向:
self.session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())
let url = NSURL(string: "yoururl")!
self.session.dataTaskWithURL(url) { (data, response, error) in
if let error = error {
NSLog("error %@ / %d", error.domain, error.code)
} else if let response = response as? NSHTTPURLResponse {
NSLog("response %d", response.statusCode)
} else {
fatalError()
}
}.resume()
func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest?) -> Void) {
NSLog("direct to %@", request.URL!)
completionHandler(request)
}
斯威夫特 3 版本:
class ViewController: UIViewController, URLSessionTaskDelegate {
var session: URLSession! = nil
override func viewDidLoad() {
super.viewDidLoad()
self.session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
let url = NSURL(string: "yoururl")!
self.session.dataTask(with: url as URL) { (data, response, error) in
if let error = error {
NSLog("error %@ / %d", (error as NSError).domain, (error as NSError).code)
} else if let response = response as? HTTPURLResponse {
NSLog("response %d", response.statusCode)
} else {
fatalError()
}
}.resume()
}
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
print("direct to %@", request.url!)
completionHandler(request)
}
}