正如很多开发人员一样,我通过http从get服务器获得一些数据。由于XCOde7 7/iOS9 9,我需要在我的应用程序plist中将使用的域标记为例外。这对我所有的域名都有效,除了一个。
重要:这不是解决方案,我接受所有域与NSAllowsArbitraryLoads。首先,我在plist中尝试了以下条目:
<key>cc.mydomain.de</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>此配置适用于所有其他域,但不适用于此域,因此我将以下条目添加到plist中:
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>但是,当我试图访问域时,仍然有一个错误。
应用程序传输安全性阻止了明文HTTP (http://)资源负载,因为它是不安全的)。可以通过应用程序的Info.plist文件配置临时异常
如果这些异常项不起作用,那么什么在起作用?这种异常的最小配置是什么?我漏掉了哪一条?
发布于 2015-10-22 07:46:18
我联系了对这个问题的支持,他们告诉我,问题是我的一个url重定向到另一个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)
}Swift 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)
}
}发布于 2015-10-08 15:07:16
试试这个:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- .......................... -->
<!-- Other keys already present -->
<!-- .......................... -->
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>mydomain.de</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
</dict>
</plist>发布于 2015-10-27 09:53:58
要从http://访问资源,需要在info.plist文件中添加以下行
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>https://stackoverflow.com/questions/32719100
复制相似问题