Login UIWebView https post request bypass ssl iOS

Hi readers hope all of you are doing good, Few days back I came across a problem if you are the one having same problem then you are on right place. Let say you have an web application where users normally can login using their login credentials and your requirement is you want to show the same web application in iOS web-view without using web application login page. Either you can get username or password using iOS native controls thats UITextfield or may be you can use Custom urls to get username and password from link.

Advertisement

Setting UIWebView

To setup UIWebView follow the following tutorial, After setting up come back and follow this article.

Setup UIWebView

In the above tutorial just follow up to setting up web view which includes making web view variables, setting web view delegate and connecting code with web view.

ViewDidload

In viewdidload you have to set UIWebView delegate, which already has been set in web view tutorial, then you have construct your target URL where you want to send post request. Then make an instance of NSMutableURL and initialize it with target URL. Then set body and http method to post, In the body you have to include all those parameter which you want to send on server in my case it was only username and password. Eventually you have to encode the data and send the NSMutableUrlRequest to webview in view did load method. This is the basic method of doing post request.

WebViewDidFinished

When the web view loading is complete you receive a response from web login, if let say in your web server you are doing a post request to a certain controller and then you want to redirect to home page, then in the following code you can redirect to home page when webviewloaded=0, so here you send again a web request so this function is called again so make sure next time  webviewloaded value is more than 0 other wise your code will stuck into infinite loop.

Bypassing SSL

If you are sending a post or get request to https server in an iOS UIWebView but you don’t have a valid certificate installed on your server. In that case web request will be failed and you might get an expection with following error, errors can be different but in short all ssl relating to certificates can be solved by using a small simple hack which is not recommended but anyhow you can use.

“The certificate for this server is invalid. You might be connecting to a server that is pretending to be “https://example.com” which could put your confidential information at risk.”

The simple solution is paste the following methods in your code, which will bypass ssl.

 DidReceiveResponse

Can Authenticate Against Protection Space

We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.

Should start load with request

Did Receive Authentication Challenge

You may be also interested in following tutorials

Login With Google iOS tutorial Using Oauth

Login With SoundCloud iOS Tutorial Using Oauth

Login With Tumblr Using oAuth

Login With Foursquare

Login with Vimeo in iOS Development

Login With Twitter Example with iOS Tutorial using Oauth

Login With Instagram

Advertisement

2 Responses

Leave a Reply