Uiwebview Does Not Load Https Page: Error Domain=Nsurlerrordomain Code=-999 "The Operation Couldn't Be Completed. (Nsurlerrordomain Error -999.)"

UIWebView does not load HTTPS Page: Error Domain=NSURLErrorDomain Code=-999 The operation couldn’t be completed. (NSURLErrorDomain error -999.)

I ran your code. It worked fine approximately 50% of the time, and showed the -999 error the other times.

That error code is NSURLErrorCancelled, as per URL Loading System Error Codes in Apple's Foundation Constants Reference. So something is cancelling the page load.

I printed the page content that was received, using this:

println(aWebView.stringByEvaluatingJavaScriptFromString("document.body.innerHTML"));

I see that the Javascript in the page contains this:

if (mobilePath) {
document.body.style.display = 'none';
document.location = mobilePath;
}

My guess is that this Javascript starts to run as soon as the content is loaded. This results in a race condition where sometimes the page is immediately redirected to a different document.location, and so the first page load is cancelled before it loads completely.

You should change the server so that it does not send this Javascript.

Alternatively, if you just want to load the website like a normal website, then you can let UIWebView handle the redirects for you. In the case of this site, I got it working properly by setting the User-agent to pretend to be mobile Safari. I also removed shouldStartLoadWithRequest because it was only interfering with things.

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

var request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
var userAgent = ["UserAgent": "mozilla/5.0 (iphone; cpu iphone os 7_0_2 like mac os x) applewebkit/537.51.1 (khtml, like gecko) version/7.0 mobile/11a501 safari/9537.53"]
NSUserDefaults.standardUserDefaults().registerDefaults(userAgent as [NSObject : AnyObject])

self.webView.loadRequest(request)
}

If I don't have the code to pretend to be mobile Safari, then the page content loads but it doesn't do the right thing. It just stops at a spinner. I presume that the website is sending different Javascript depending on which rendering engine it thinks it is talking to. If you don't specify any User-agent then you are at the mercy of whatever their web designer has done in terms of specifying the default behavior.

UIWebView won't load(Error -999)

This appears to be a bug in iOS with HTTPS requests. A workaround for me was redoing the request when the error is given. The second time the request seemed to load fine and it works perfectly now.

NSURLErrorDomain error code -999 in iOS

The error has been documented on the Mac Developer Library(iOS docs)

The concerned segment from the documentation will be:

URL Loading System Error Codes


These values are returned as the error code property of an NSError
object with the domain “NSURLErrorDomain”.

enum
{
NSURLErrorUnknown = -1,
NSURLErrorCancelled = -999,
NSURLErrorBadURL = -1000,
NSURLErrorTimedOut = -1001,

As you can see; -999 is caused by ErrorCancelled. This means: another request is made before the previous request is completed.

How do I fix NSURLErrorDomain error -999 in iPhone 3.0 OS

I was able to find the answer here.

This thread contained this description for this error: This error may occur if an another request is made before the previous request of WebView is completed...

I worked around this by ignoring this error and letting the webview continue to load.

if ([error code] != NSURLErrorCancelled) {
//show error alert, etc.
}

Error -999 when loading multiple webViews

I spent weeks worrying about this error. I was getting it randomly while accessing web pages. In my case I put it down to pages being requested too quickly back to back as the web access was driven by a state machine in code and not by a user.

After much searching, in the end I found a few discussions which could not explain why the error was occuring, but it was felt that it was feature of UIWebView rather than something you should worry about. The guidance was to ignore it. I will see if I can find the article and update this answer later if I can find it.

I updated my code as follows, and so far have seen no ill effects at all since adding it. This would suggest it is almost a notification and anything which causes it seems to get corrected inside UIWebView. Hopefully this is the same in your case.

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
VSSLog(@"Entry: error = %@",error);

// Added this based on net advice. Its a bogus error.
if ([error code] == NSURLErrorCancelled) {
return;
}

... Normal error handling code for proper errors.
}

I am not one for out of sight out of mind, but this I believe is one of those cases where it is ok.

Finally if you are using iOS8 only, you could try moving to use the new WKWebView rather than UIWebView.



Related Topics



Leave a reply



Submit