3

I have a site I'm loading with a UIWebView that has some problems loading when secured with Basic Authtype of Apache:

NSString * myUrlAddress = [NSString stringWithFormat:@"http://user:pass@mysite.mydomain.com"];
NSURL *url = [NSURL URLWithString:myUrlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
[webView loadRequest:requestObj];

Initial loading works most of the times, but sometimes, especially on reloading of the app the callback

- (void)webViewDidFinishLoad:(UIWebView *)webViewloc

is not reached, and it is also not running into

-(void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error

If I use a different server without .htaccess to secure the page it all works fine.

I also see in the access log, that it sometimes just stops loading from the page.

Has this something to do with cachePolicy or timeoutInterval ?

spankmaster79
  • 21,555
  • 10
  • 42
  • 73

2 Answers2

0

You could try to manage the authentication challenge coming from the server on your own, since it seems that UIWebView is not able to handle it.

This requires setting up your own NSURLRequest/Connection and handle the challenge in the connection:didReceiveAuthenticationChallenge delegate method before trying to load the actual html page in your UIWebView.

You can find full sample code here for a GET request. You might also check into a third-party networking framework that makes handling the communication and the challenge easier (e.g., ASIHTTPRequest, although it is now in a "frozen" state it works well; or AFNetworking)

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • UIWebView can't open a window and ask. But it accepts the username:password URL. It's just that it sometimes takes up to 3 times to open the site in the app to fully load... – spankmaster79 Jan 24 '12 at 11:07
  • I would just give a try to the method I suggest, possibly it would remove any "uncertainty" ("up to three times") from the loading process. – sergio Jan 24 '12 at 11:28
  • @sergio I am also facing the same issue but in random. I felt that your answer would help me in fixing the issue. But I didn't get you how to setup NSURLRequest/NSURLConnection to a web view. In my code i have created a NSURLRequest object with the Url and called loadRequest on my web view obj. Can you plz elaborate on setting up own connection object which would help me a lot. Thanks in advance – Dinakar Dec 10 '13 at 12:36
  • @Dinakar: you get a full example about using your own NSURLConnection with a UIWebView here: http://stackoverflow.com/questions/1769888/how-to-display-the-authentication-challenge-in-uiwebview – sergio Dec 10 '13 at 12:54
  • @sergio Thank you for the help and seems working well. Unfortunately I happened to restore my settings in "Settings" app and now I am in a situation to check the code solves the actual prob. :) restoring settings resolved the issue without the above code changes...But I can't request my users to do so..hence integrated the code and wanted to check if that works..Final question is there a way to test the code (reproduce the issue and check the code solves the issue) – Dinakar Dec 11 '13 at 01:36
0
NSString * myUrlAddress = [NSString stringWithFormat:@"http://user:pass@mysite.mydomain.com"];
NSURL *url = [NSURL URLWithString:myUrlAddress];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
Vibhooti
  • 1,193
  • 2
  • 9
  • 20