89

I've been trying to use Corona SDK's Facebook API to post the score on the game I'm developing on facebook. However, I'm having a problem with it. During the first time I try to post to facebook, I get this error after login and user authentication:

NSURLErrorDomain error code -999

Then, it won't post on facebook. What are possible causes of this error and how can I address it?

By the way, I am not using webview on my app. Just the widget api and a show_dialog listener in my Facebook class.

starball
  • 20,030
  • 7
  • 43
  • 238
user1597438
  • 2,171
  • 5
  • 35
  • 78

12 Answers12

149

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.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • 5
    Can mean (error.code == NSURLErrorCancelled == -999) and error.domain (NSString) is NSURLErrorDomain . You get this when you cancel a task using cancel – Ryan Heitner Dec 28 '14 at 11:25
  • Hello hjpotter92, My app crashes when this error occurred any help? – Gopal Devra Jun 15 '15 at 05:12
  • 20
    Wanted to add that you get same error (-999) when server's SSL certificate has issues (i.e it is being expired or invalid) – arrteme Apr 11 '17 at 14:52
  • @arrteme It shouldn't. The code is -12xx for those: https://developer.apple.com/reference/foundation/1508628-url_loading_system_error_codes/nsurlerrorservercertificatenotyetvalid?language=objc – hjpotter92 Apr 11 '17 at 15:01
  • 1
    @hjpotter92 I could reference to this answer http://stackoverflow.com/a/40100179/3065769; I was receiving -999 Canceled Error in console, but performing same POST request via curl I got a message that said that there were SSL issues on server; I don't have much expertise in this area though – arrteme Apr 11 '17 at 15:47
  • @arrteme hmm strange. – hjpotter92 Apr 11 '17 at 18:45
  • Guys can you clear this out? Is this an SSL certificate issue or is it because another request is made before the previous request is completed.? – iPhoneDeveloper Dec 28 '17 at 10:22
  • @iPhoneDeveloper https://developer.apple.com/documentation/foundation/nsurlerrorcancelled For SSL certificate issue, the errors raised are [`nsurlerrorclientcertificaterejected`](https://developer.apple.com/documentation/foundation/nsurlerrorclientcertificaterejected) and [`nsurlerrorclientcertificaterequired`](https://developer.apple.com/documentation/foundation/nsurlerrorclientcertificaterequired) – hjpotter92 Dec 28 '17 at 12:05
  • So -999 refers to a request being made before the previous request is completed right? – iPhoneDeveloper Dec 29 '17 at 06:54
  • 8
    I just got -999 and I've an invalid SSL certificate. I fixed the certificate and the -999 is gone. – Florian Cargoet Aug 10 '18 at 10:15
  • @arrteme thanks for pointing in a right direction. My SSL pinning was failing apparently – AzaFromKaza Jul 29 '19 at 10:48
  • This can be raised by a implementation of SSL certificate pinning. – BuguiBu Dec 23 '19 at 07:19
  • @hjpotter92 'another request is made before the previous request is completed.' - So we can't make two requests immediately one after the other? – Kiran Jasvanee Apr 22 '20 at 12:32
14

Just wanted to add here, when receiving a -999 "cancelled" the problem usually is one of two things:

  • You're executing the exact same request again.
  • You're maintaining a weak reference to your manager object that gets deallocated prematurely. (Create strong reference)
Ramon
  • 1,415
  • 9
  • 18
  • 1
    So, what's wrong with the exact same request? I just want to refresh page. – Kimi Chiu Jan 22 '19 at 17:37
  • 2
    I should elaborate, I meant executing the exact same request while the same object is already “executing” (=e.g. downloading data). The first request is then cancelled in order to execute the second. – Ramon Jan 23 '19 at 18:24
  • So if I use a `POST` method to send the exact same `JSON` string to the same address, it will be cancelled? In my case, it just cancel it even if the previous request is done. – Kimi Chiu Jan 24 '19 at 15:21
  • @Chris Graf: I have reversed your edit since I'd like to maintain the simplicity of this answer. I do agree with your input though. Please do really post your comment as a separate answer for others to see! :) – Ramon Aug 23 '19 at 18:55
  • @Ramon no worries, I added the third option in [a separate answer](https://stackoverflow.com/questions/16073519/nsurlerrordomain-error-code-999-in-ios/57636132#57636132), thanks for the update! – Christopher Graf Aug 24 '19 at 07:58
11

hjpotter92 is absolutely right, I just want to provide solution for my case. Hopefully it is useful for you as well. Here is my situation:

On log in page > press log in > pop up loading dialog > call log in service > dismiss dialog > push another screen > call another service --> cause error -999

To fix it, I put a delay between dismissing dialog and pushing new screen:

    [indicatorAlert dismissWithClickedButtonIndex:0 animated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                [self performSegueWithIdentifier:@"HomeSegue" sender:nil];
            });

It is strange that this issue happens on iOS 7 only.

thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
  • 1
    I didn't see this in 10 but after upgrading to 11, I see it. So it's hard to tell if its a false positive or other. I haven't found anything wrong with the behavior of the app either. – Micah Montoya Oct 20 '17 at 14:12
  • Does anyone know how to do this in swift? – Sam Apr 24 '19 at 21:53
  • the code you present has nothing to do with NSURLSession, or the error described by the OP. Error codes are not unique - they are only unique within their domain (NSURLDomain). how can any UI trickery cause an NSURLSession task to fail? – Motti Shneor Oct 16 '19 at 08:32
7

I have faced the same error with Alamofire and it was because the certificate pinning. The certificate wasn't valid anymore, so I had to remove it and add the new one. Hope it helps.

Pablo Blanco
  • 679
  • 8
  • 14
3

In addition to what Ramon wrote, there is a third possible reason when receiving a NSURLErrorDomain -999 cancelled:

You cancelled the task while it was executing either by calling .cancel() on the datatask object or because you used .invalidateAndCancel() on the session object. If you are creating a custom session with a delegate, you should call .invalidateAndCancel() or .finishTasksAndInvalidate() to resolve the strong reference between the session and its delegate, as mentioned in the Apple Developer Documentation:

The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you don’t invalidate the session, your app leaks memory until it exits.

If you are wondering about this logging behaviour, I found the following explanation in the Apple Developer forums:

By way of explanation, back in iOS 10 we introduced a new logging system-wide logging architecture (watch WWDC 2016 Session 721 Unified Logging and Activity Tracing for the details) and lots of subsystem, including CFNetwork, are in the process of moving over to that. Until that move is fully finished you’re going to encounter some weird edge cases like this one.

Christopher Graf
  • 1,929
  • 1
  • 17
  • 34
2

I didn't use Corona SDK's Facebook API but I encountered this problem when using Alamofire, the secondRequest always cancel in execution with the error -999, according to the posts I found on internet, the reason is that session property is deinit before completion of async work since it is out of the scope, I finally solved this problem by deinit the session property manually so the compiler won't deinit it at wrong position:

class SessionManager {
    var session:SessionManager?

    init() {
        self.session = SessionManager(configuration:URLSessionConfiguration.ephemeral)
    }
    private func firstRequest() {
        guard let session = self.session else {return}
        session.request(request_url).responseData {response in
            if let data=response.data {
                self.secondRequest()
            }
    }
    private func secondRequest() {
        guard let session = self.session else {return}
        session.request(request_url).responseData {response in
            if let data=response.data {
                self.secondRequest()
            }
            //session will no longer be needed, deinit it
            self.session = nil
    }

    }
ilovecomputer
  • 4,238
  • 1
  • 20
  • 33
2

Our company's app has many -999 error in iOS. I have searched around, find the reason has two, like the network task has been dealloc or the certificate isn't valid. But I have checked our code, these two aren't possible. I am using Alamofire which is using URLSession. Luckily, our company's android app's network is normal. So we check the difference. We found the http request from iOS is Http2.0, while android is Http1.1. So we force the backend http support version down to http1.1, then -999 error count descends!!!

I think there maybe some bug in Apple's URLSession. Check the link New NSURLSession for every DataTask overkill? for some detail thoughts

Dan Lee
  • 101
  • 1
  • 5
1

Please check If you call cancel() on URLSessionDataTask to fix

NSURLErrorDomain Code=-999 "cancelled"
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
0

I was getting this error in iOS specific version of Xamarin app. Not sure the underlying cause, but in my case was able to work around it by using post method instead of get for anything passing the server context in the request body -- which makes more sense anyway. Android / Windows / the service all handle the GET with content, but in iOS app will become partially unresponsive then spit out the 999 NSUrlErrorDomain stuff in the log. Hopefully, that helps someone else running into this. I assume the net code is getting stuck in a loop, but could not see the code in question.

0

For my Cordova project (or similar), turns out it was a plugin issue. Make sure you're not missing any plugins and make sure they're installed properly without issue.

Easiest way to verify this is simply to start fresh by recreating the Cordova project (cordova create <path>) along with the required platforms (cordova platform add <platform name>) and add each plugin with the verbose flag (--verbose) so that you can see if anything went wrong in the console log while the plugin is being downloaded, added to project and installed for each platform (cordova plugin add cordova-plugin-device --verbose)

Recap: cordova create <path> cordova platform add <platform name> cordova plugin add cordova-plugin-device --verbose

Vyrnach
  • 119
  • 1
  • 9
0

For my case, I used an upload task post that did not need body contents:

// The `from: nil` induces error "cancelled" code -999
let task = session.uploadTask(with: urlRequest, from: nil, completionHandler: handler)

The fix is to use zero byte data instead of nil,

let task = session.uploadTask(with: urlRequest, from: Data(), completionHandler: handler)

The framework documentation doesn't specify why the from bodyData is an optional type, or what happens when it is nil.

dlalpine
  • 51
  • 2
-1

We solved this problem by reloading the web view when it failed loading.

extension WebViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        webView.reload()
    }
}