2017-03-08 10 views
1

私のSwift Mac OSアプリケーションをローカルホスト上のサーバー上で実行しているページに接続しようとすると、このエラーが発生します。私はサーバーが稼動していることを確認しました。
これは私のInfo.plistファイルです:NSURLSession/NSURLConnection HTTP読み込みに失敗しました(kCFStreamErrorDomainSSL、-9843) - Mac OS app

<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> 

そして、これは、ページにアクセスしようとするコードの一部です。それは迅速なアプリからです。プロジェクトをビルドするときにエラーはありません。私はローカルホストと17.83.148.252を交換しようとしたが、それは動作しません

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() 
    } 

:スウィフトのこのバージョンは、それがURLSession、URLRequestのに変更し、そうでしまったNSURLを持っていないように私はNSURLを使用することはできません。ここで
は、詳細なエラーの説明です:

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

はい、私はすでに私のInfo.plistを見てみましたthat.Please、キーNSAppTransportSecurityはthere.Iは、これに対する解決策を発見されました。問題は、私はMac OSサーバーを使用してサーバーでPHPファイルを実行していたことです。興味深いことに、localhost/test.phpを実行していたときにブラウザからは動作していましたが、同じマシン上にある私の素早いプログラムからは動きませんでした。私はこのリンクをの代わりにhttps://username-macbook-air.local/test.phpというコードで使用しました。

<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でスウィフト3との安全な接続を必要としていることを考え出し:私はlocalhostを使用する場合、アクセスは私が含まれていても、「サーバーが証明書を持っていない」と言ってブロックされている、ように思えますSSL証明書はデフォルトでそこにありますが、正しいリンクを見つけるだけです。

関連する問題