2016-04-06 14 views
6

Ho un URL https://203.xxx.xxx.xxx. Questo URL viene utilizzato per il nostro Solo per il test.UIWebview.loadURL() ma l'URL è un certificato autofirmato. Swift iOS

Voglio caricare questo URL in UIWebview in Swift2.0. Ma ahimè ! il certificato è scaduto.

Ho fatto lo stesso con successo in Android, ma avendo problemi in Swift.

Si prega di guida .....


Che cosa ho fatto fino ad ora !!


01. Defined AppTransportSecurity in info.plist 

    <key>NSAppTransportSecurity</key> 
      <dict> 
       <key>NSAllowsArbitraryLoads</key> 
       <true/> 
      </dict> 
  1. Provato utilizzando la connessione (canAuthenticateAgainstProtectionSpace) & connessione (willSendRequestForAuthenticationChallenge)

Ma non è riuscito.


Cosa accade quando carico lo stesso URL in Safari ??


errore dice: Safari cannot verify the IDENTITY

bisogno di aiuto per aggirare questo.


provato anche alcuni link: (Questo potrebbe essere la soluzione)

@implementation NSURLRequest (NSURLRequestWithIgnoreSSL) 

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

@end 

Ma come implementare questo in Swift 2.0? Se ho torto, guida correttamente in Swift.

risposta

7

finalmente ottenuto la risposta come:

Fase 1 >>import SafariServices

Fase 2 >> Usa NSURLConnectionDelegate con i tuoi ViewController cioè

class ViewController:UIViewController, NSURLConnectionDelegate

Passo metodi 3 >> Override:

func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool 

func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) 

Fase 4 >> GOTO viewDidLoad dove per caricare il tuo URL nel Webview, & apportare le modifiche come:

let url = NSURL (string: URL)//where URL = https://203.xxx.xxx.xxx 
let requestObj = NSURLRequest(URL: url!) 
var request: NSURLRequest = NSURLRequest(URL: url!) 
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)! 
connection.start() 
YOUR_WEBVIEW.loadRequest(requestObj); 

Fase 5 >> GOTO WebView con shouldStartLoadWithRequest & return true. Se restituisci false, non otterrai mai i risultati.

func webView(IciciWebView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool 
{ 
//Do whatever you want to do. 

return true 
} 

Fase 6 >> Aggiornare la funzione:

func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool 
    { 
     print("In canAuthenticateAgainstProtectionSpace"); 

     return true; 
    } 

Passo 7 >> Aggiornare la funzione:

func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) 
    { 
     print("In willSendRequestForAuthenticationChallenge.."); 
     challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge) 
     challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge) 

    } 

Referenze: referenze Developer di Swift, Lotto di pagine di StackOverflow, & alcuni forum di Google.

Questi passaggi risolvono i miei problemi & ora è possibile caricare l'URL autofirmato in una visualizzazione Web.

+2

Nel passo 4, perché sono presenti sia 'request' che' requestObj'? – kdazzle

Problemi correlati