以下目标-C代码的等效Swift代码是什么?
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];发布于 2015-06-01 02:43:08
这在Foundation框架中是不可用的。它来自,苹果,,私有API和苹果说,如果您使用私有API,您的应用程序将被拒绝。
发布于 2015-09-26 17:22:14
溶液
将此添加到文件info.plist中,这将强制ATS接受自签名证书:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/> -->
</dict>
</dict>
</dict>然而,正如解释这里一样,这是不够的。在要使用NSURLSession的每个类中,都必须声明以下委托:
class LoginService: NSObject, NSURLSessionDelegate {
func URLSession(session: NSURLSession,
task: NSURLSessionTask,
didReceiveChallenge challenge: NSURLAuthenticationChallenge,
completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?)
-> Void) {
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
}
...}
然后执行如下操作:
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let urlRequest: NSURLRequest = NSURLRequest(URL: requestURL)
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue())
/*
dataTaskWithRequest: creates an HTTP request for the specified URL request object, and calls a handler upon completion.
*/
let task = session.dataTaskWithRequest(urlRequest...)这对我有用。请记住,您必须使用ApacheTL2.4.x,因为它是唯一支持 1.2的版本。希望这能帮上忙。
https://stackoverflow.com/questions/30564912
复制相似问题