Last active
April 12, 2017 07:27
-
-
Save CaliosD/9e4262d0151764f3a07b32dc2ce90eff to your computer and use it in GitHub Desktop.
URL validation category for NSString, including both ip and DNS address types.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Calios on 2017-04-10. | |
// Copyright © 2017 Calios. All rights reserved. | |
// | |
@implementation NSString (URLValidate) | |
- (BOOL)isValidURL | |
{ | |
if ([self isIPAddress]) { | |
return [self isValidIPAddress]; | |
} | |
return [self isValidDNSAddress]; | |
} | |
- (BOOL)isValidDNSAddress { | |
NSString *result = self; | |
if (![self containsProtocol]) { | |
result = [NSString stringWithFormat:@"http://%@",self]; | |
} | |
NSString *urlRegEx = | |
@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+(:[0-9]{1,})?"; | |
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; | |
return [urlTest evaluateWithObject:result]; | |
} | |
- (BOOL)isIPAddress { | |
NSString *result = self; | |
if ([self containsProtocol]) { | |
result = [result componentsSeparatedByString:@"//"].lastObject; | |
} | |
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"[a-zA-Z]" options:0 error:NULL]; | |
NSInteger matches = [regex numberOfMatchesInString:result options:0 range:NSMakeRange(0, result.length)]; | |
return (matches <= 0); | |
} | |
- (BOOL)isValidIPAddress { | |
NSString *result = self; | |
if ([self containsProtocol]) { | |
result = [result componentsSeparatedByString:@"//"].lastObject; | |
} | |
NSString *ipRegEx = @"(\\d{1,3}\\.){3}(\\d){1,3}(:\\d{1,})?"; | |
NSPredicate *ipTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", ipRegEx]; | |
return [ipTest evaluateWithObject:result]; | |
} | |
- (BOOL)containsProtocol { | |
return [self hasPrefix:@"http://"] || [self hasPrefix:@"https://"]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Calios on 2017-04-10. | |
// Copyright © 2017 Calios. All rights reserved. | |
// | |
#import <XCTest/XCTest.h> | |
#import "NSString+URLValidate.h" | |
@interface URLValidationTests : XCTestCase | |
@end | |
@implementation URLValidationTests | |
NSString *url1 = @"http://192.168.1.9"; | |
NSString *url2 = @"http://192.168.1.9:1010"; | |
NSString *url4 = @"192.168.1.9:1010"; | |
NSString *url5 = @"192.168.1.9"; | |
NSString *urlInvalid1 = @"http://192.168.1..9"; | |
NSString *urlInvalid2 = @"192.168.1..9"; | |
NSString *urlInvalid3 = @"http://192.168.1.9:101x"; | |
NSString *urlInvalid4 = @"http://#(@*&%#@)$(*&%#@.com"; | |
tring *url7 = @"http://office.shtdtech.com:28080"; | |
NSString *url8 = @"http://office.shtdtech.com"; | |
NSString *url9 = @"office.shtdtech.com"; | |
NSString *url0 = @"office.shtdtech.com:48080"; | |
- (void)setUp { | |
[super setUp]; | |
// Put setup code here. This method is called before the invocation of each test method in the class. | |
} | |
- (void)tearDown { | |
// Put teardown code here. This method is called after the invocation of each test method in the class. | |
[super tearDown]; | |
} | |
- (void)testValidIPAddress { | |
XCTAssertTrue([url1 isValidIPAddress]); | |
XCTAssertTrue([url2 isValidIPAddress]); | |
XCTAssertTrue([url4 isValidIPAddress]); | |
XCTAssertTrue([url5 isValidIPAddress]); | |
XCTAssertFalse([urlInvalid1 isValidIPAddress]); | |
XCTAssertFalse([urlInvalid2 isValidIPAddress]); | |
XCTAssertFalse([urlInvalid3 isValidIPAddress]); | |
} | |
- (void)testValidDNSAddress { | |
XCTAssertTrue([url6 isValidDNSAddress]); | |
XCTAssertTrue([url7 isValidDNSAddress]); | |
XCTAssertTrue([url8 isValidDNSAddress]); | |
XCTAssertTrue([url9 isValidDNSAddress]); | |
XCTAssertTrue([url0 isValidDNSAddress]); | |
XCTAssertFalse([url6 isIPAddress]); | |
XCTAssertFalse([url7 isIPAddress]); | |
XCTAssertFalse([url8 isIPAddress]); | |
XCTAssertFalse([url9 isIPAddress]); | |
XCTAssertFalse([url0 isIPAddress]); | |
} | |
- (void)testValidURL { | |
XCTAssertTrue([url1 isValidURL]); | |
XCTAssertTrue([url2 isValidURL]); | |
XCTAssertTrue([url4 isValidURL]); | |
XCTAssertTrue([url5 isValidURL]); | |
XCTAssertTrue([url6 isValidURL]); | |
XCTAssertTrue([url7 isValidURL]); | |
XCTAssertTrue([url8 isValidURL]); | |
XCTAssertTrue([url9 isValidURL]); | |
XCTAssertTrue([url0 isValidURL]); | |
XCTAssertFalse([urlInvalid1 isValidURL]); | |
XCTAssertFalse([urlInvalid2 isValidURL]); | |
XCTAssertFalse([urlInvalid3 isValidURL]); | |
XCTAssertFalse([urlInvalid4 isValidURL]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add test case.