My iPhone application includes several HTTP requests to the server. The IP address of the server can be entered by the user, so you can use the application in combination with your own personal server.
Before making requests, I always check to see if the IP address I entered is valid, and I do it as follows:
-(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 fine, as long as the address you enter 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. Therefore, for some reason I need to check whether the entered address looks like an “IP address”, and I do not know how to do this.
Any suggestions are welcome!