Hello!
HTTP requests to localhost using the LAN IP address return error POSIX 2 "No such file or directory". It happens with NSURLConnection and NSURLSessionDataTask. It started happening when I installed OSX El Capitan (final release) and only on iOS 9 and tvOS 9 simulators. iOS 8 works perfectly, iOS 9 and tvOS 9 devices work fine too.
Before El Capitan, with Yosemite, there was no problem on any simulator.
You can try to reproduce it with this code:
NSError *error = nil;
NSURLResponse *response = nil;
NSURLRequest *requestLocalhost = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:9001"]]; // localhost
NSURLRequest *request127IP = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1:9001"]]; // localhost through LAN IP address
NSURLRequest *requestLocalhostLANIP = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.59:9001"]]; // localhost through LAN IP address
NSURLRequest *requestOtherLANIP = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.35:9001"]]; // localhost through LAN IP address
[NSURLConnection sendSynchronousRequest:requestLocalhost returningResponse:&response error:&error];
NSLog(@"Error through localhost: %@", error);
error =nil;
[NSURLConnection sendSynchronousRequest:request127IP returningResponse:&response error:&error];
NSLog(@"Error through 127.0.0.1: %@", error);
error =nil;
[NSURLConnection sendSynchronousRequest:requestLocalhostLANIP returningResponse:&response error:&error];
NSLog(@"Error through LAN IP address: %@", error);
error =nil;
[NSURLConnection sendSynchronousRequest:requestOtherLANIP returningResponse:&response error:&error];
NSLog(@"Error to other LAN IP address: %@", error);
Just start an HTTP server somewhere in your computer (running "python -m SimpleHTTPServer 9001" in your Terminal) and also in another computer. Replace "192.168.0.59" with your machin IP address, and "192.168.0.35" with the other machin IP address.
This is the output I get:
Error through localhost: (null)
Error through 127.0.0.1: (null)
Error through LAN IP address: Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSErrorFailingURLStringKey=http://192.168.0.59:9001/, NSErrorFailingURLKey=http://192.168.0.59:9001/, _kCFStreamErrorCodeKey=2, _kCFStreamErrorDomainKey=1}
Error to other LAN IP address: (null)
Again, the same happens with NSURLSessionDataTask. Any help is welcome!
Thank you in advance 🙂