Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebview.loadURL() but URL is a self-signed certificate. Swift iOS

I have a URL https://203.xxx.xxx.xxx. This URL is used for our Testing purposes only.

I want to load this URL in UIWebview in Swift2.0. But alas ! the certificate is expired.

I did the same successfully in Android, but having issues in Swift.

Please guide.....


What did i do till now !!


01. Defined AppTransportSecurity in info.plist

    <key>NSAppTransportSecurity</key>
            <dict>
                <key>NSAllowsArbitraryLoads</key>
                <true/>
            </dict>
  1. Tried using connection(canAuthenticateAgainstProtectionSpace) & connection(willSendRequestForAuthenticationChallenge)

But didn't succeed.


What happens when i load the same URL in Safari ??


Error says : Safari cannot verify the IDENTITY

Need help in bypassing this.


Also tried some links : (This could be the solution)

@implementation NSURLRequest (NSURLRequestWithIgnoreSSL) 

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

@end

But how to implement this in Swift 2.0 ? If i am wrong please guide correctly in Swift.

like image 379
Pawan Avatar asked Apr 06 '16 03:04

Pawan


1 Answers

Finally got the answer as :

Step 1 >> import SafariServices

Step 2 >> Use NSURLConnectionDelegate with your ViewController i.e.

class ViewController:UIViewController, NSURLConnectionDelegate

Step 3 >> Override methods :

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

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

Step 4 >> GOTO viewDidLoad where to load your URL in the Webview, & make changes as :

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

Step 5 >> GOTO webView with shouldStartLoadWithRequest & return true. If you return false, you are never-ever getting the results.

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

 return true
}

Step 6 >> Update the function :

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

        return true;
    } 

Step 7 >> Update the function :

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

    }

References : Developer references of Swift, Lot of Pages of Stackoverflow, & some forums of Google.

These steps fixed my issues & now i can load the self signed URL in a web view.

like image 91
Pawan Avatar answered Oct 21 '22 11:10

Pawan