Search code examples
iosobjective-ciphonensurlconnectionnsurlrequest

NSURLResponse comes quickely in Simulator but on actual Device it comes after some time or returns NULL


I am creating a application which calls Web service on every view-controller.The Application runs fine on Simulator but on actual device, it runs very slowly or sometimes it returns null response that's why app crashes due to null response from sever.Is there any solution to solve this issue.I am very new to objective-c programming.Please suggest me a way to solve the issue. Thanks a lot.:)

here I am adding my part of the code which sends request to a web-service and receives response.

{
    NSURL *url = [NSURL URLWithString:@"http://www.someurl.com/somewebservice.asmx"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    [theRequest addValue:@"somehost.in" forHTTPHeaderField:@"Host"];
    [theRequest addValue:@"someUrl/someWebMethod" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Type"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection =
    [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    NSURLResponse *response1 = [[NSURLResponse alloc] init];;
    self.webResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];

    self.response = [[NSURLResponse alloc] init];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Printing response...\n");
    NSLog(@"%@",response);
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.webResponseData  appendData:data];
}

Solution

  • You are using sendSynchronousRequest. Don't. Never, never, never network synchronously on the main thread.