Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate an IP address with regular expression in Objective-C?

How to validate an IP address in Objective-C?

like image 317
diana Avatar asked Nov 05 '09 08:11

diana


1 Answers

Here's a category using the modern inet_pton which will return YES for a valid IPv4 or IPv6 string.

    #include <arpa/inet.h>

    @implementation NSString (IPValidation)

    - (BOOL)isValidIPAddress
    {
        const char *utf8 = [self UTF8String];
        int success;

        struct in_addr dst;
        success = inet_pton(AF_INET, utf8, &dst);
        if (success != 1) {
            struct in6_addr dst6;
            success = inet_pton(AF_INET6, utf8, &dst6);
        }

        return success == 1;
    }

    @end
like image 114
Evan Schoenberg Avatar answered Sep 28 '22 00:09

Evan Schoenberg