2

I'm trying to connect to a service using https://. When I'm entering correct user credentials it's working well. But when I'm entering invalid user credentials, It's giving me an error,

Error Domain=NSURLErrorDomain Code=-1012 "(null)" UserInfo={NSErrorFailingURLStringKey=https://api.myapp.com/myapp/mobile/signIn, NSUnderlyingError=0x1018f0530 {Error Domain=kCFErrorDomainCFNetwork Code=-1012 "(null)" UserInfo={_kCFURLErrorAuthFailedResponseKey={url = https://api.myapp.com/myapp/mobile/userCredentialNotMatching}}}, NSErrorFailingURLKey=https://api.myapp.com/myapp/mobile/signIn}

When this error is coming, all http header and response is nil.

NSString *body = [NSString stringWithFormat:@"email=%@&password=%@&device=%@&platform=%@&authType=%@&latitude=%@&longitude=%@", email, password, deviceId, PLATFORM, APP_TYPE, latitude, longitude];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSLog(@"response status code: %ld", (long)[httpResponse statusCode]);

    NSDictionary *allHeaderFields;
    allHeaderFields = [(NSHTTPURLResponse *) response  allHeaderFields];

    if (data.length > 0 && connectionError == nil)
    {
        if ((long)[httpResponse statusCode] == 200)
        {
            NSError *jsonError = nil;
            NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

            NSLog(@"response status code: %@", newStr);

            NSDictionary *responceDic =[NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];

            //                NSMutableArray *convertedDiscussion =[[NSMutableArray alloc] init];

            [defaults setObject:email forKey:USER_DEFAULTS_KEY_EMAIL];
            [defaults setObject:password forKey:USER_DEFAULTS_KEY_PASSWORD];
            [defaults setObject:[responceDic objectForKey:@"nickName"] forKey:USER_DEFAULTS_KEY_USERNAME];
            [defaults setObject:[responceDic objectForKey:@"profileImgUrl"] forKey:USER_DEFAULTS_KEY_PROFILE_PIC];
            [defaults setObject:[responceDic objectForKey:@"isPremiumUser"] forKey:USER_DEFAULTS_KEY_PREMIUM];

                [defaults setObject:[[responceDic objectForKey:@"userMetaData"] objectForKey:@"noOfActivities"] forKey:USER_DEFAULTS_KEY_META_DATA];


            [defaults synchronize];

            [[Branch getInstance] setIdentity:[NSString stringWithFormat:@"%@", [responceDic objectForKey:@"ninjaName"]]];
            BTDiscussion *convertedDiscussion = [self discussionObjectsFromJson:responceDic];

            completion ([allHeaderFields objectForKey:NINJA_MESSAGE],convertedDiscussion, connectionError);
        }else
        {

            completion (@"Your account is still not activated. Please check your email and verify the email address to activate the account.",nil, connectionError);

        }
    }
    else
    {
        completion (@"Your account is still not activated. Please check your email and verify the email address to activate the account.", nil, connectionError);
    }
}];

Here what I did to log in to the app. How may I fix this?

codebot
  • 2,540
  • 3
  • 38
  • 89
  • What's to fix? If the connection cannot be established (due to authentication failure) then no HTTP request/response messages are exchanged. – trojanfoe Jan 07 '16 at 11:00
  • What is this issue? This is working with the correct credentials – codebot Jan 07 '16 at 11:04
  • It's just a question of knowing what you can access in that completion block when the request succeeds and when it fails. I expect the first thing you need to look at is the `NSError` parameter, and do different things based on whether it's set or not. – trojanfoe Jan 07 '16 at 11:20
  • Maybe this post can help you http://stackoverflow.com/questions/28069651/nsurlerrordomain-code-1012-the-operation-couldn-t-be-completed – Pipiks Jan 07 '16 at 12:41

0 Answers0