gpt4 book ai didi

ios - 尝试使用 swift 从自签名服务器获取数据,用于 iOS 应用程序开发

转载 作者:行者123 更新时间:2023-11-28 08:04:54 25 4
gpt4 key购买 nike

首先我尝试了 info.plist 方式,使用

让我的网站成为一个异常(exception)
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>service.medimetry.dev</key>
<dict>
<key>NSRequiresCertificateTransparency</key>
<false/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
</dict>
</dict>

仍然显示错误“发生 SSL 错误,无法与服务器建立安全连接。”在尝试了 5 种不同的上层代码变体之后,我开始寻找另一个角度。我尝试使用此代码通过 swiftHTTP 方式绕过 SSL 身份验证

func getAllReminders () {
do {
let opt = try HTTP.GET("https://service.medimetry.dev/api/v2/reminders")
var attempted = false
opt.auth = { challenge in
if !attempted {
attempted = true
return URLCredential(forTrust: challenge.protectionSpace.serverTrust)
}
return nil
}

opt.start { response in
if let err = response.error {
print("error: \(err.localizedDescription)")
return //also notify app of failure as needed
}
print("opt finished: \(response.description)")

print("Response Loggin starts >>>")
print(response.data)
print("<<< Response Loggin ends")
}
} catch let error {
print("got an error creating the request: \(error)")
}
}

我收到此错误““challenge.protectionSpace.serverTrust”与可用重载不匹配”试图找到解决方法,但无法解决。

然后用 google 搜索 http 的所有包并找到 Alamofire也有同样的问题,尝试谷歌搜索并发现这个写这个代码

func getAllReminders () {
defaultManager.request("https://service.medimetry.dev/api/v2/reminders").responseJSON { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result

if let json = response.result.value {
print("JSON: \(json)") // serialized json response
}

if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}
}

let defaultManager: Alamofire.SessionManager = {
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"127.0.0.1:443": .disableEvaluation,
"service.medimetry.dev:443": .disableEvaluation,
"https://service.medimetry.dev:443": .disableEvaluation,
"127.0.0.1:80": .disableEvaluation,
"service.medimetry.dev:80": .disableEvaluation,
"https://service.medimetry.dev:80": .disableEvaluation,
]

let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders

return Alamofire.SessionManager(
configuration: configuration,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
}()

但又是这个回应2017-08-03 16:29:34.369747+0530 提醒系统 [5364:433698] [] nw_coretls_callback_handshake_message_block_invoke_3 tls_handshake_continue: [-9812]2017-08-03 16:29:34.369 提醒系统 [5364:433712] NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9813)请求:可选(https://service.medimetry.dev/api/v2/reminders)回应:无结果:失败数据:

我在这个问题上花了好几个小时,请告诉我我做错了什么。上述代码中的任何修复,或任何其他解决方法,在投入生产时我将拥有所有正确的服务器,但只需要在 Debug模式下解决。在 alamofire 我试过 this .另外,如果有使用 justHTTP pod 的简单方法。对于服务器,我正在使用带有安全标志的 laravel 代客。

最佳答案

只需在您的 plist 中添加这些键

<key>NSIncludesSubdomains</key> <true/> 
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key> <string>TLSv1.1</string>

它应该适用于自签名证书

关于ios - 尝试使用 swift 从自签名服务器获取数据,用于 iOS 应用程序开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45482423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com