首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSAppTransportSecurity:不使用正确的设置

NSAppTransportSecurity:不使用正确的设置
EN

Stack Overflow用户
提问于 2015-09-22 14:06:30
回答 4查看 1.7K关注 0票数 3

正如很多开发人员一样,我通过http从get服务器获得一些数据。由于XCOde7 7/iOS9 9,我需要在我的应用程序plist中将使用的域标记为例外。这对我所有的域名都有效,除了一个。

重要:这不是解决方案,我接受所有域与NSAllowsArbitraryLoads。首先,我在plist中尝试了以下条目:

代码语言:javascript
复制
<key>cc.mydomain.de</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>

此配置适用于所有其他域,但不适用于此域,因此我将以下条目添加到plist中:

代码语言:javascript
复制
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
     <false/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
     <true/>
<key>NSExceptionRequiresForwardSecrecy</key>
    <false/>

但是,当我试图访问域时,仍然有一个错误。

应用程序传输安全性阻止了明文HTTP (http://)资源负载,因为它是不安全的)。可以通过应用程序的Info.plist文件配置临时异常

如果这些异常项不起作用,那么什么在起作用?这种异常的最小配置是什么?我漏掉了哪一条?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-10-22 07:46:18

我联系了对这个问题的支持,他们告诉我,问题是我的一个url重定向到另一个url,当然,这个url在我的plist中也必须被列为例外。您可以使用url会话委托方法轻松地确定重定向:

代码语言:javascript
复制
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版本:

代码语言:javascript
复制
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)
     }
}
票数 1
EN

Stack Overflow用户

发布于 2015-10-08 15:07:16

试试这个:

代码语言:javascript
复制
<!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>
票数 0
EN

Stack Overflow用户

发布于 2015-10-27 09:53:58

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

代码语言:javascript
复制
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32719100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档