14

I'm new to objective-c and i'm having a hard time with a AFNetworking.

So the thing is that i want to make a simple POST request to a server who will send me back a salt. I'v make a simple app, in order to test my request but i don't understand why i'm getting the error code 999.

Here a sample of my code.

+ (void)simpleRequest; {
    NSURL *mailserver = [NSURL URLWithString:@"https://localhost:4443/"];

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:mailserver];
    manager.securityPolicy.allowInvalidCertificates = TRUE;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSDictionary *parameters = @{@"username": @"testtest"};

    [manager POST:@"api/v0/login/salt" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

I' have link this code to a simple Button which's calling this function.

I have an other app, in ruby motion which's work fined with this function i can get the response without any error. But with this simple app i can't do any request, they all returned this error code 999.

Error: Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://localhost:4443/api/v0/login/salt, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://localhost:4443/api/v0/login/salt}

So i'm really wondering what i'm doing wrong, anyone can help me on this ? Thanks

EDIT:

Is it the good way for saving the manager in a property or am i doing something wrong ? If it's the good way, this seems to not work Thanks for the help

.h file

@property (nonatomic, retain) AFHTTPSessionManager *session;

.m file

@synthesize session;
- (IBAction)log:(id)sender {
NSURL *mailserver = [NSURL URLWithString:@"https://localhost:4443/"];

self.session = [[AFHTTPSessionManager alloc]initWithBaseURL:mailserver];
self.session.securityPolicy.allowInvalidCertificates = TRUE;
self.session.responseSerializer = [AFJSONResponseSerializer serializer];
self.session.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"username": @"testtest"};

[self.session POST:@"api/v0/login/salt" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
xikimay
  • 167
  • 1
  • 1
  • 11
  • It's showing that your URL is error...maybe should convert localhost to your IP address? – Tj3n Oct 15 '15 at 10:43
  • And sometime the params will be in the request body and that param field is nil: like [https://localhost:4443/api/v0/login/salt?username=testtest](https://localhost:4443/api/v0/login/salt?username=testtest) – Tj3n Oct 15 '15 at 10:58
  • I just tried but it doesn't work, at least thanks for the help – xikimay Oct 15 '15 at 11:00
  • In my case it should be a request like that 'https://localhost:4443/api/v0/login/salt', Which's work fined in my other app – xikimay Oct 15 '15 at 11:02
  • i did some searched in Google and they said its because of previous one request hasn't complete and the other one sends , thus u get the Cancelled error – Tj3n Oct 15 '15 at 11:18
  • ah ok, but it's weird because this is the only request that i'v done. – xikimay Oct 15 '15 at 12:07
  • Still unsolve, maybe i have to initialize something before making a request ? – xikimay Oct 15 '15 at 16:37

5 Answers5

25

In my case iOS 10 SDK's caused AFNetworking error code -999. If you're trying to reach a server that has SSL and you don't want to check it out, add some privacy Policy to Afnetworking

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;

[securityPolicy setValidatesDomainName:NO];
Kirby
  • 15,127
  • 10
  • 89
  • 104
LittleBoat
  • 341
  • 3
  • 4
  • 1
    A little more context, like where to assign this security policy, or whether it's a singleton, etc, would be helpful. This answer by itself isn't really enough. – shortstuffsushi Jan 03 '17 at 05:12
  • Fwiw, the premise of this answer is useful -- setting the security policy _on the session manger_ does help (at least in my case). – shortstuffsushi Jan 03 '17 at 05:15
  • 1
    _sessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:_sessionConfiguration]; AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; securityPolicy.allowInvalidCertificates = YES; [securityPolicy setValidatesDomainName:NO]; _sessionManager.securityPolicy = securityPolicy; – munibsiddiqui Feb 16 '17 at 12:04
  • I'm getting an error about `AFSSLPinningModeNone` being ambiguous (AFNetworking 3.1) (solved it using `[AFSecurityPolicy defaultPolicy]` which internally calls the same thing anyway). – Can Poyrazoğlu May 11 '17 at 07:28
  • This didn't entirely work, getting this warning now: "In order to validate a domain name for self signed certificates, you MUST use pinning". – iosdude Aug 08 '17 at 09:19
12

That's error -999, not 999. That is NSURLErrorCancelled. Your request has been cancelled before it can be completed.

Looking at your code, you aren't retaining the AFHTTPSessionManager *manager anywhere. This means that the manager will be disposed as soon as +simpleRequest returns. I'm guessing that this is what is cancelling your request.

You need to save the manager so that it lives for the full duration of the request. Save it in a property somewhere.

Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39
  • I think i have correctly save the manager but it's still failing, did i have miss something ? – xikimay Oct 19 '15 at 08:20
  • @xikimay then it's because the instance owning your property is deinitialised before the response gets back. A hack might be to call the manager within the block (through a `Log()`) for example. – Nico Aug 24 '16 at 10:20
3

In my case iOS 9 SDK's "App transport security" cause AFNetworking error code : -999. If you're trying to reach a server that doesn't have a SSL add keys like screenshot below.

enter image description here

EFE
  • 3,732
  • 4
  • 22
  • 30
  • 4
    This should not be an accepted answer anymore. As of iOS 10, allowing arbitrary loads requires a review from Apple and specific permission to do so. The better answer is to use ATS to allow loads to HTTP locations by using exceptions. View this post, https://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http, for the real solution to this problem. – Olyve Jul 28 '17 at 18:47
0

I noticed that your API endpoint indicates to a secure connection:

httpS://localhost:4443/api/v0/login/salt

Just try it just in case, maybe it repeats your situation.

In my case, this was a typo in the API manager code. Which from the part can be said is connected with App Transport Security Settings.

Just changed the protected protocol from httpS:// to http:// and the error:

NSURLErrorDomain Code = -999 "cancelled"

was gone and it all worked!

+And also if you had a similar problem. Be sure to discuss this with a backend specialist who deals with the server or API configuration for your application. This means that the server does not have valid security certificates. Perhaps you still need a secure connection. Or this specialist can again configure everything back from http:// to httpS://, and I'm not sure (did not check) whether this will work again when in the code you are already using a non-secure http:// connection.

CAHbl463
  • 102
  • 7
0

[Original OP question may not mention Cordova but this link was very high in the search result when Googling this issue]

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