ios - 使用 Alamofire 将 iOS 应用程序连接到在 localhost 上运行的 api 时证书无效
<p><p>我正在尝试连接到在 localhost 上运行的 API,以便可以在 iOS 模拟器中测试我的应用程序。我来了</p>
<blockquote>
<p>NSLocalizedDescription=The certificate for this server is invalid.
You might be connecting to a server that is pretending to be “127.0.0.1”
which could put your confidential information at risk.,
NSErrorFailingURLKey=<a href="https://127.0.0.1:8000/post/" rel="noreferrer noopener nofollow">https://127.0.0.1:8000/post/</a>,
NSErrorFailingURLStringKey=<a href="https://127.0.0.1:8000/post/" rel="noreferrer noopener nofollow">https://127.0.0.1:8000/post/</a>,
NSErrorClientCertificateStateKey=0</p>
</blockquote>
<p>我正在使用 <strong>Alamofire</strong>。 <a href="https://stackoverflow.com/questions/30163274/how-to-connect-localhost-with-invalid-certificate-using-alamofire" rel="noreferrer noopener nofollow">This similar question</a>没有帮助。与旧版本的 Alamofire 相比,它似乎已经过时了。</p>
<p>我的 info.plist 已经包含</p>
<pre><code><key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</code></pre>
<p>如何暂时禁用证书要求,以便在 localhost 上测试我的应用程序?</p>
<p>这是我在更改服务器信任策略后的代码,如其中一个答案所建议的那样</p>
<p> ViewController :</p>
<pre><code>class TableViewController: UIViewController {
let postClient = PostServiceClient.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
postClient.getPosts()
}
}
</code></pre>
<p>PostServiceClient:</p>
<pre><code>import Alamofire
class PostServiceClient {
static let sharedInstance: PostServiceClient = PostServiceClient()
var sessionManager : SessionManager!
init() {
let serverTrustPolicies: = [
"https://127.0.0.1:8000/" : .disableEvaluation
]
self.sessionManager =SessionManager(configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
}
static let url = URL.init(string: "https://127.0.0.1:8000/post/")
// Method to get posts from the wall
func getPosts(){
print("Getting posts with completion handler")
var request = URLRequest(url: PostServiceClient.url!)
request.httpMethod = "GET"
self.sessionManager.request(request).responseJSON { (response) in
guard response.result.isSuccess else {
print("Error while getting posts: \(String(describing: response.result.error))")
return
}
guard let responseJSON = response.result.value as? ,
let results = responseJSON["results"] as? [] else {
print("Invalid response recieved from service")
return
}
print(responseJSON)
}
}
}
</code></pre>
<p>这是我得到的完整输出:</p>
<blockquote>
<p>Getting posts with completion handler 2017-06-19 14:22:15.770616-0400
WallAppiOS []
nw_coretls_callback_handshake_message_block_invoke_3
tls_handshake_continue: [-9812] 2017-06-19 14:22:15.770
WallAppiOS NSURLSession/NSURLConnection HTTP load
failed (kCFStreamErrorDomainSSL, -9813) Error while getting posts:
Optional(Error Domain=NSURLErrorDomain Code=-1202 "The certificate for
this server is invalid. You might be connecting to a server that is
pretending to be “127.0.0.1” which could put your confidential
information at risk." UserInfo={NSLocalizedDescription=The certificate
for this server is invalid. You might be connecting to a server that
is pretending to be “127.0.0.1” which could put your confidential
information at risk., NSLocalizedRecoverySuggestion=Would you like to
connect to the server anyway?, _kCFStreamErrorDomainKey=3,
NSUnderlyingError=0x7a3627c0 {Error Domain=kCFErrorDomainCFNetwork
Code=-1202 "(null)"
UserInfo={_kCFStreamPropertySSLClientCertificateState=0,
_kCFNetworkCFStreamSSLErrorOriginalValue=-9813, _kCFStreamErrorCodeKey=-9813, _kCFStreamErrorDomainKey=3, kCFStreamPropertySSLPeerTrust=,
kCFStreamPropertySSLPeerCertificates=(
"" )}}, _kCFStreamErrorCodeKey=-9813, NSErrorFailingURLStringKey=<a href="https://127.0.0.1:8000/post/" rel="noreferrer noopener nofollow">https://127.0.0.1:8000/post/</a>,
NSErrorPeerCertificateChainKey=(
"" ), NSErrorClientCertificateStateKey=0,
NSURLErrorFailingURLPeerTrustErrorKey=,
NSErrorFailingURLKey=<a href="https://127.0.0.1:8000/post/" rel="noreferrer noopener nofollow">https://127.0.0.1:8000/post/</a>})</p>
</blockquote></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在此示例中,我使用 <code>serverTrustPolicyManager</code> 来处理与 ssl 未认证服务器的连接,我使用单例来处理我的应用程序中的所有连接,您必须声明 <code>sessionManager</code>正如 Alamofire github 页面所说的那样</p>
<blockquote>
<p>Make sure to keep a reference to the new SessionManager instance,
otherwise your requests will all get cancelled when your
sessionManager is deallocated.</p>
</blockquote>
<pre><code> class exampleNetworkClient {
static let sharedInstance: exampleNetworkClient = exampleNetworkClient()
var sessionManager : SessionManager?
init() {
let serverTrustPolicies: = [
"https://127.0.0.1:8000" : .disableEvaluation
]
self.sessionManager =SessionManager(configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
}
static let portalUrl = URL.init(string:"https://127.0.0.1:8000/exampleserviceUrl")
func exampleMethod()
{
var request = URLRequest(url: iOSETKClient.portalUrl!)
request.httpMethod = "GET"
//Important Note that you need to use your custom session manager
self.sessionManager!.request(request).responseString { (response) in
///...RESPONSE LOGIC...///
}
}
}
</code></pre>
<p>希望对你有帮助</p></p>
<p style="font-size: 20px;">关于ios - 使用 Alamofire 将 iOS 应用程序连接到在 localhost 上运行的 api 时证书无效,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44621671/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44621671/
</a>
</p>
页:
[1]