当我尝试将Swift Mac OS应用程序连接到我的localhost上的服务器上运行的页面时,我收到此错误。我确保服务器已启动并运行 这是我的info.plist文件:
<plist version="1.0">
<dict>
<key>UIBackgroundModes</key>
<array>
<string></string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
<string>Location Service always in use</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string></string>
</array>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSMainStoryboardFile</key>
<string>Main</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>NSLocationWhenInUseDescrciption</key>
<string>Location Needed</string>
<key>CFBundleIconFile</key>
<string></string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>17.83.148.252</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2017 Meghalee. All rights reserved.</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>NSAllowsArbitraryLoads</key>
<string>YES</string>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<string></string>
</dict>
</plist>
这是尝试访问该页面的代码的一部分。它来自迅捷的应用程序。构建项目时没有错误。我无法使用NSURL,因为此版本的Swift没有NSUrl,它已更改为URLSession,URLRequest等等:
private func sendtoPHP(nSt : NetworkStatistics,lat : Double, long : Double )
{
var request = URLRequest(url: URL(string: "https://17.83.148.252/test.php")!)
request.httpMethod = "POST"
// let postString = "a=\(nSt.getCurrentSsid()!)&b=\(nSt.getRssiValue()!)&c=\(nSt.getNoiseMeasurement()!)&d=\(nSt.getWlanChannel()!)&e=\(nSt.getBssid()!)&f=\(nSt.getCountryCode()!)&g=\(nSt.getHardwareAddress()!)&h=\(nSt.getTransmitPower()!)&i=\(lat)&j=\(long)"
let postString = "a=\(nSt.getCurrentSsid()!)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
}
我尝试用localhost替换17.83.148.252,但它不起作用 以下是详细的错误说明:
2017-03-08 11:53:40.453196 NetworkHealth_Mac[56146:753522] Unfiltered exception: SSLHostname
2017-03-08 11:53:40.479160 NetworkHealth_Mac[56146:753489] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
error=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 “17.83.148.252” which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey= NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “17.83.148.252” which could put your confidential information at risk., NSErrorFailingURLKey=https://17.83.148.252/test.php, NSErrorFailingURLStringKey=https://17.83.148.252/test.php, NSErrorClientCertificateStateKey=0})
答案 0 :(得分:0)
是的我已经尝试过了。请查看我的info.plist,关键的NSAppTransportSecurity就在那里。我找到了解决方法。问题是,我使用Mac OS服务器在服务器中运行PHP文件。有趣的是,当我运行localhost / test.php时,它是从浏览器工作但不是从我的swift程序(在同一台机器上)。我在我的代码中使用了这个链接:https://username-macbook-air.local/test.php而不是{{3} }。似乎,当我使用localhost时,访问被阻止说&#34;服务器没有证书&#34; ,即使我包括:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
所以我认为OS X中的Swift 3需要与SSL证书建立安全连接,默认情况下我们必须找到正确的链接。