【问题标题】:NSAppTransportSecurity: Not working with correct settingsNSAppTransportSecurity:不使用正确的设置
【发布时间】:2015-12-19 13:30:18
【问题描述】:

由于那里有许多开发人员,我通过 http 从网络服务器获取一些数据。从 XCOde7/iOS9 开始,我需要在我的应用程序列表中将使用的域标记为例外。这适用于我的所有域,除了一个域。

重要提示:我无法通过 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/>

但我在尝试访问域时仍然遇到错误。

App Transport Security 已阻止明文 HTTP (http://) 资源加载,因为它不安全。可以通过应用的 Info.plist 文件配置临时异常

如果这些异常条目不起作用,那么什么是起作用的?像这样的异常的最低配置是什么。我错过了哪个条目?

【问题讨论】:

    标签: ios9 xcode7 nsapptransportsecurity


    【解决方案1】:

    试试这个:

    <!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>
    

    【讨论】:

    • 嘿。感谢您的回复。不幸的是,它也不起作用。目前我认为问题不在于 plist 配置,而是 url 或 web 服务本身,因为它指的是图像渲染 ASP.NET 服务
    【解决方案2】:

    我就这个问题联系了 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)
    }
    

    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)
         }
    }
    

    【讨论】:

      【解决方案3】:

      要从 http:// 访问资源,您需要在 info.plist 文件中添加以下行

      <key>NSAppTransportSecurity</key>
      <dict>
          <key>NSAllowsArbitraryLoads</key>
          <true/>
      </dict>
      

      【讨论】:

      • 请阅读我所说的问题:重要提示:我无法接受所有具有 NSAllowsArbitraryLoads 的域。
      【解决方案4】:

      我添加了应用程序传输安全设置,但错误仍然出现在控制台中。

      当我在 UIWebView 中运行 Web url 时,我的应用程序显示此错误。如果您的页面有一些内部 url/frame 添加到您的 info.plistException Domains 则错误将出现在控制台中。

      在我的例子中,我在UIWebView 中调用了一个网页,而这个页面有一个 facebook 插件。此网页已正确加载,但也在控制台中显示此错误。当我彻底检查时,我发现在我的应用程序中我使用了 Facebook 登录,并且在这个 Facebook 教程中,我指示我在 info.plist 中添加 facebook.com 作为 Exception Domains

      当我删除这个Exception Domains 时,错误消失了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-14
        • 2011-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多