Abhi
Abhi

Reputation: 243

IOS: kCFErrorDomainCFNetwork error -1002. Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"

hello all i know this kind of question asked previously but i didn't get any solution from them in my project i am working in login view when i am put code on login button i am getting an error

Error : Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x7fb37b62c9a0 {NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x7fb37b715a20 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1002.)"}

but i am using the same code for login which i used in my previous projects and it works fine there

here is my code:

-(IBAction)login:(id)sender
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:@"http://eyeforweb.info.bh-in-15.webhostbox.net/myconnect/api.php?token={LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUc4d0RRWUpLb1pJaHZjTkFRRUJCUUFEWGdBd1d3SlVBeWo0WE9JNjI4cnJRTG9YeEpXNG1zUWI1YmtvYk1hVQpzMnY1WjFKeXJDRWdpOVhoRzZlZk4rYTR0eGlMTVdaRXdNaS9uS1cyL1NCS2pCUnBYUzVGYUdiV0VLRG1WOXkvCkYrWHhsUXVoeER0MEV3YkRBZ01CQUFFPQotLS0tLUVORCBQVUJMSUMgS0VZLS0tLS0K}&method=user.getLogin"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"*/*" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    NSString *mapData = [NSString stringWithFormat:@"[email protected]&password=123456"];//,username.text, password.text];
    NSData *postData = [mapData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    [request setHTTPBody:postData];
    NSLog(@"map data is = %@",mapData);
    NSURLSessionDataTask * postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse* response, NSError * error) {

        if(error == nil)
        {
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
            NSString *text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
            NSLog(@"Data = %@",text);
            NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
            NSLog(@"jsondic= %@",jsonDic);

            NSDictionary *userDataDic = [jsonDic objectForKey:@"data"];
            NSLog(@"Dict is %@",userDataDic);

Please help me to resolve it i already see the similar type of questions but didn't overcome from this issue Any help is appreciated

Upvotes: 2

Views: 4615

Answers (1)

user3182143
user3182143

Reputation: 9609

I tried your code.Except your url the other lines of code is correct.If you pass your corrct URL,it works perfectly.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://eyeforweb.info.bh-in-15.webhostbox.net/myconnect/api.php"]]; //pass your url here
 [request setHTTPMethod:@"POST"];
 //Passing The String to server
 NSString *strUserId = @"[email protected]";
 NSString *strPassword = @"admin123";


 NSString *userUpdate =[NSString stringWithFormat:@"login=%@&password=%@",strUserId,strPassword, nil];

 //Check The Value what we passed
 NSLog(@"the data Details is %@", userUpdate);


 //Convert the String to Data
 NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
 NSLog(@"The postData is - %@",data1);


 //Apply the data to the body
 [request setHTTPBody:data1];

 NSURLSession *session = [NSURLSession sharedSession];
 NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
 if(httpResponse.statusCode == 200)
 {
 NSError *parseError = nil;
 NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
 NSLog(@"The response is - %@",responseDictionary);
 NSInteger success = [[responseDictionary objectForKey:@"success"] integerValue];
 if(success == 1)
 {
 NSLog(@"Login SUCCESS");
 }
 else
 {
 NSLog(@"Login FAILURE");
 }
 }
 else
 {
 NSLog(@"Error");
 }
 }];
 [dataTask resume];

Upvotes: 1

Related Questions