10

i calling web service from my ios app using NSURLConnection. here is my code

#define kBaseServerUrl       @"http://winshark.softwaytechnologies.com/bariodwcfservice/baroidservice.svc/Json/"

#define kProductServiceUrl @"Products"

-(void)callService{
    NSURL *url;
    if (currState == ProductServiceState) {
        url = [NSURL URLWithString:[NSString  stringWithFormat:@"%@%@/%d",kBaseServerUrl,kProductServiceUrl,[self getRevisionNumberForProduct]]];
    }
    else if(currState == TankStrickerServiceState){
        url = [NSURL URLWithString:[NSString  stringWithFormat:@"%@%@/%d",kBaseServerUrl,kTankStrickerServiceUrl,[self getRevisionNumberForTankStricker]]];
    }else if(currState == CalculationServiceState){
        url = [NSURL URLWithString:[NSString  stringWithFormat:@"%@%@/%d",kBaseServerUrl,kCalculationServiceUrl,[self getRevisionNumberForCalculation]]];
    }else{
        //Future Use
    }
    NSLog(@"%@",[url absoluteString]);
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

    NSLog(@"%@",urlConnection);
}

Its end up calling this url http://winshark.softwaytechnologies.com/bariodwcfservice/baroidservice.svc/Json/Products/123

but when i hit this url in browser its working fine, but in my app i m getting this error

Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server."

Please help me what might be problem

sachin
  • 1,015
  • 4
  • 11
  • 24

1 Answers1

2

Looks like you are setting it up right, I would suspect something regarding your objects - try calling a hard coded URL by replacing your existing URL request with:

NSURLRequest *urlRequest = [NSURLRequest 
requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];

Should make troubleshooting this much easier.

woody121
  • 358
  • 3
  • 14
  • it's weird, I can't find `requestWithString` method in [NSURLRequest class documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html) ... can you help me please? – Maksym Gontar Jan 17 '14 at 10:41
  • I think the best help I can give you is point you to some good other SO code ;-) check out http://stackoverflow.com/questions/19263186/nsurlrequest-creation-using-query-string-in-ios for examples of URL requests made this way. – woody121 Jan 17 '14 at 16:45
  • 1
    @MaxGontar There isn't one. I think he meant `[NSURLRequest requestWithURL:[NSURL URLWithString:@"…"]]`. – devios1 Mar 18 '15 at 17:41
  • 1
    @devios thanks, was just going on memory and didn't catch the mismatch – woody121 Mar 18 '15 at 21:20