Search code examples
web-servicesios6info-plist

Cannot access web service in iOS


I want to connect with a web service from my iOS app. Previously my URL was http://<domain name>/mobilews/mobilews.asmx. Recently I changed to http://<domain name>/mobilewstest/mobilews.asmx

I have put my URL in info plist file. But after I changed this into new URL I cannot login to that.

NSString *urlString = [NSString stringWithFormat:@"%@%@",serverURL,[queryString  stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];


NSHTTPURLResponse *response ;

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

int statusCode = [((NSHTTPURLResponse *)response) statusCode];`

Here returnData become nill and statusCode is 0. But this urlString is successfully logged in to the web service when it gives in the browser.


Solution

  •     NSURL *url = [NSURL URLWithString:@"YOUR URL HERE"];
        NSError *connectionError = nil;
        NSData *inData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&connectionError];
        NSInteger code = [connectionError code];
        if (code != 0)
        {
            NSString *locDesc = [NSString stringWithString:[connectionError localizedDescription]];
            NSString *locFail = [NSString stringWithString:[connectionError localizedFailureReason]];
            NSLog(@"Error: %d %@ %@", code, locDesc, locFail);
        }
        else if ([inData length] == 0)
        {
            NSLog(@"No data");
        }
        else{
        NSData *resultData  = [NSData dataWithContentsOfURL:url];
        NSString *responseString = [[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding];
        }
    

    I see you send a syncronous request... try this its more an easy approach.