3

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!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Chris
  • 391
  • 2
  • 6
  • 17
  • "1234" is invalid, because it's identical to "0.0.4.210", but "0.0.0/8" is reserved. The lowest valid address (in decimal notation) is 16777217. But "dotted decimal" is not the only valid representation of an IPv4 address. – MSalters Mar 01 '11 at 13:02

2 Answers2

7

You can check url validity from below method :

- (BOOL) validateUrl: (NSString *) candidate {
    NSString *urlRegEx =
    @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; 
    return [urlTest evaluateWithObject:candidate];
}
Mitesh Khatri
  • 3,935
  • 4
  • 44
  • 67
  • I have not really checked, but wouldn't this falsely recognize for instance 10.10.10.10.10 as valid? – jv42 Mar 01 '11 at 13:28
  • This doesn't works with URL having hyphen"-" sign. :( –  Feb 17 '14 at 11:30
  • this falsely recognize strings where each component is larger than 256. Obviously, a regex is the *wrong* idea here. Not to mention IPv6 – Jean-Denis Muys Oct 06 '15 at 13:35
2
-(BOOL)isIPAddressValid:(NSString*)ipAddress{

ipAddress = [ipAddress stringByReplacingOccurrencesOfString:@"https://" withString:@""];
ipAddress = [ipAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];

NSArray *components = [ipAddress componentsSeparatedByString:@"."];
if (components.count != 4) {
    return NO;
}
NSCharacterSet *unwantedCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
if ([ipAddress rangeOfCharacterFromSet:unwantedCharacters].location != NSNotFound){
    return NO;
}
for (NSString *string in components) {
    if ((string.length < 1) || (string.length > 3 )) {
        return NO;
    }
    if (string.intValue > 255) {
        return NO;
    }
}
if  ([[components objectAtIndex:0]intValue]==0){
    return NO;
}
return YES;

}

MrSmith
  • 21
  • 2