6

I want to open an URL which need password and username in a UIWebview. Such as open my local Wifi Router(192.168.1.1). But when I try following code, there is no popup as Safari to require password and username.

NSURL *url = [NSURL URLWithString:@"http://192.168.1.1"];
NSURLRequest *httpReq = [NSURLRequest requestWithURL:url];
[self._webView loadRequest:httpReq];

Since someone told me to use NSURLConnectionDelegate, I know this, but I donot know how to show the authorized page to the UIWebView.

ZYiOS
  • 5,204
  • 3
  • 39
  • 45
  • Try adding the Authorization field, like described here: http://stackoverflow.com/questions/1973325/nsurlconnection-and-basic-http-authentication – SVD Aug 16 '11 at 02:50

3 Answers3

7

This will help

NSURL *url = [NSURL URLWithString:@"http://username:password@192.168.1.1"]; NSURLRequest *httpReq = [NSURLRequest requestWithURL:url]; [self._webView loadRequest:httpReq];

kingemerald
  • 156
  • 9
3

You need to implement connection:didReceiveAuthenticationChallenge: in NSURLConnectionDelegate. For details, read Authentication Challenges chapter from Apple "URL Loading System Programming Guide".

siuying
  • 1,449
  • 12
  • 15
1

UIWebView doesn't provide any mechanism to identify the response. So one solution is explained in UIWebViewHttpStatusCodeHandling github project which identifies the status code of the response (should be 404 in your case).

However, the main drawback is for each request you need to use NSURLConnection and load the request again. But in this case, you can cancel the NSURLConnection too.

The other solution should (might) be the use of Java-Script. Search for the Java-script, which provides the status code of the response.

Sagar
  • 3,159
  • 3
  • 29
  • 28