2015-09-30 5 views
11

専門家とのHTTPSサイトにアクセスすることはできませんHTTPSプロトコルでWebアプリケーションに送信します。 05:13.879 Webページテスター[2947:31838] 私のアプリを構築しながら、以下の私のコードは、以下のエラーメッセージスウィフト2 - Xcodeの7.0は、私はIOS 9/XCODE私はiOSアプリを作成しようとしてい</p> <p>7 /スウィフト2開発キット単にルートで初心者だ、unstrusted SSL証明書

2015年10月1日01を示しViewController.swift

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet var myWebView: UIWebView! 

    /** 
    * Function to Display the Web Application initial URL 
    */ 
    func loadAppURL(){ 
     let siteAddress = "https://domain:8443/path/to/page" 
     let url = NSURL (string: siteAddress) 
     let urlRequest = NSURLRequest(URL: url!) 
     myWebView.loadRequest(urlRequest) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     loadAppURL() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

で、これまでですNSURLSession/NSURLConnection HTTP負荷が (kCFStreamErrorDomainSSL、-9807)

を失敗し、私は "http://www.apple.com" との代わりに "https://domain:8443/path/to/page" の私のアプリを構築しようとした場合、その正常に動作します。

私はSafariでWebアプリケーションにアクセスでき、セキュリティ上のリスクを受け入れるよう求めています。私はそれを受け入れ、私は自分のアプリケーションにアクセスすることができます。

私の問題を修正するためのガイドです。ありがとうございます。

+0

これは、http://stackoverflow.com/questions/20365774/how-to-call-https-url-in-uiwebview/20789128#20789128 http://stackoverflow.com/questions/8858674/allow-unverified-ssl-certificate-in-uiwebview – jrc

+0

@jrcあなたが言及した質問は、特定のアイオス9ではなく、ソリューションは迅速でもない、他の手がかりですか? –

答えて

8

を以下のように追加することができます最後に、私はXcodeのは、デフォルトでは、サーバからの信頼でき、自己署名証明書を拒否します

それを修正しました。

NSURLConnectionを使用してこれをオーバーライドすることができ、UIWebViewでは使用できない追加のデリゲートメソッドを使用して認証を制御できるため、自己署名付きサーバーと通信できます。したがって、connection:didReceiveAuthenticationChallengeを使用すると、自己署名入りサーバーに対して認証を行うことができます。

参照 NSURLAuthenticationChallenge Docs、@Lilo呂のQuestion以下の手順では、私は私の問題を解決しました


ステップ1:

override func viewDidLoad() { 
    super.viewDidLoad() 

    let siteAddress = "https://domain:8443/path/to/page" 
    let url = NSURL (string: siteAddress) 
    let urlRequest = NSURLRequest(URL: url!) 
    let urlConnection:NSURLConnection = NSURLConnection(request: request, delegate: self)! 
    myWebView.loadRequest(urlRequest) 
} 

を次のようには私viewController.swiftのNSURLConnectionviewDidLoad()方法を定義手順2: NSURLConnectionデリゲートメソッドを使用

func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{ 
     print("canAuthenticateAgainstProtectionSpace method Returning True") 
     return true 
    } 


    func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){ 

     print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)") 

     if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { 
      print("send credential Server Trust") 
      let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!) 
      challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge) 

     }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{ 
      print("send credential HTTP Basic") 
      let defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession) 
      challenge.sender!.useCredential(defaultCredentials, forAuthenticationChallenge: challenge) 

     }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{ 
      print("send credential NTLM") 

     } else{ 
      challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge) 
     } 
} 

となっています。

+2

回答がうまくいきませんでしたが、実際には強制的なアンラッピングの使用をやめてください。 –

+1

ブロック。 –

+0

これを試してみて、私のために働かなかった。私が使用したコードは次のとおりです:http://pastebin.com/pd2HFWfy NSURLSession/NSURLConnection HTTPのロードに失敗しました(kCFStreamErrorDomainSSL、-9813) –

4

あなたのPLISTに

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
</dict> 

enter image description here

+3

私はInfo.plistにコードを追加しましたが、エラーの変更はありません前に同じエラーが発生しました –

+1

辞書のブール値であることを確認しましたか?私がちょうど入れた編集のように見える必要があります – Mika

+2

はい、私のXcodeに表示されているものと同じですが、ここで述べた解決方法を試しますhttp://stackoverflow.com/questions/20365774/how-to-call-https-url- in-uiwebview/20789128#20789128 –

-2

編集Info.plistファイル、追加:

  1. のApp交通セキュリティ設定
  2. 値が、それは私のために働く、XCodeの7だYES

で、任意のロードを許可します。3

0

の1-作成カテゴリ「NSURLRequestCategory」 - >インポートのXcodeで作成したブリッジファイルへこのカテゴリの後に(あなたがいない持っていけない場合はXcodeの、1つを作成してみましょうすることを忘れないでください)、このコードを置く:

@implementation NSURLRequest (NSURLRequestCategory) 

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host 
{ 
    return YES; 
} 
@end 

通常、あなたのロード要求のUIWebViewを作成します。

webView.delegate = self 
let myURL = URL(string: Constants.URL_DOMAINE) 
let request = URLRequest(url: myURL!) 
webView.loadRequest(request) 

Enjoyyy:D

1

スウィフト3.で

ステップ1.メソッドを上書きするために、ビューコントローラにNSURLConnectionDelegateを追加します。

class ViewController: UIViewController, NSURLConnectionDelegate { 

ステップ2.上書きのviewDidLoad

override func viewDidLoad() { 
     super.viewDidLoad() 

     let siteAddress = "https://mysiteaddress" 
     let url = URL(string: siteAddress) 
     let urlRequest = URLRequest(url: url!) 
     let urlConnection:NSURLConnection = NSURLConnection(request: urlRequest, delegate: self)! 
     webView.loadRequest(urlRequest) 
    } 

ステップ3上書きcanAuthenticateAgainstProtectionSpacedidReceive challenge

func connection(_ connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: URLProtectionSpace) -> Bool { 
    print("\ncanAuthenticateAgainstProtectionSpace method Returning True\n") 
    return true 
} 

func connection(_ connection: NSURLConnection, didReceive challenge: URLAuthenticationChallenge) { 
    print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)") 

    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { 
     print("\nsend credential Server Trust\n") 
     let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!) 
     challenge.sender!.use(credential, for: challenge) 

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{ 
     print("send credential HTTP Basic") 
     let defaultCredentials: URLCredential = URLCredential(user: "user", password: "password", persistence:URLCredential.Persistence.forSession) 
     challenge.sender!.use(defaultCredentials, for: challenge) 

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{ 
     print("\nsend credential NTLM\n") 

    } else{ 
     challenge.sender!.performDefaultHandling!(for: challenge) 
    } 

} 

おかげナバスバシール元の溶液のために!私に時間を節約しました

関連する問題