My iPhone application includes several http requests to a server. The server's IP address can be entered by the user so you can use the app in combination with your own private server.
Before making the requests I always check whether or not the IP address entered is valid and I do it like this:
-(BOOL)urlExists {
NSString *url = [NSString stringWithFormat:@"%@", ipAddress];
NSURLRequest *myRequest1 = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
NSHTTPURLResponse* response = nil;
NSError* error = nil;
[NSURLConnection sendSynchronousRequest:myRequest1 returningResponse:&response error:&error];
if ([response statusCode] == 404){
return NO;
}
else{
return YES;
}
[url release];
[response release];
[error release];
[myRequest1 release];
}
This works perfectly as long as the entered address looks something like this: xx.xx.xxx.xxx But if you try to enter something like this, "1234" or "test", the code shown above does not work. So I somehow have to check if the entered address "looks" like an IP-address and I have no idea how to do this.
Any suggestions are greatly appreciated!