简体   繁体   中英

how to open url need username and password with uiwebview?

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.

This will help

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM