NSString stringWithContentsOfURL application crash when host is unreachable

This is a really tricky issue, I am using the code as below:

NSError *error = nil; NSString *guideText = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; 

The code works like a charm if I don’t list the server that the URL is pointing to. It gives about 1 minute of a pause, but then the method call returns with an error. So far, so good. It works the same way when I run the application in a simulator or on a device under the debugger. But if I run the program on the device without a debugger, it will work after a pause. I tried to wrap the code in @ try / @ catch, but nothing helps. In fact, the debugger does not see any problems, so there’s a mystery for me. I tried to set the security code as follows:

 NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; if ([NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]) // here code above [request release]; 

The code behaves exactly the same as the previous one. The program pauses here for a minute, and then runs normally, unless it works on a device without a debugger.

+4
source share

All Articles