Skip to content

Commit

Permalink
Merge pull request #132 from JessThrysoee/reachabilityWithURL
Browse files Browse the repository at this point in the history
Add reachabilityWithURL
  • Loading branch information
tonymillion authored Apr 6, 2019
2 parents f4d4457 + 9a20add commit ee2ae91
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions Reachability.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ typedef void (^NetworkReachability)(Reachability * reachability, SCNetworkConnec
+(instancetype)reachabilityForInternetConnection;
+(instancetype)reachabilityWithAddress:(void *)hostAddress;
+(instancetype)reachabilityForLocalWiFi;
+(instancetype)reachabilityWithURL:(NSURL*)url;

-(instancetype)initWithReachabilityRef:(SCNetworkReachabilityRef)ref;

Expand Down
33 changes: 33 additions & 0 deletions Reachability.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,39 @@ +(instancetype)reachabilityForLocalWiFi
return [self reachabilityWithAddress:&localWifiAddress];
}

+(instancetype)reachabilityWithURL:(NSURL*)url
{
id reachability;

NSString *host = url.host;
BOOL isIpAddress = [self isIpAddress:host];

if (isIpAddress)
{
NSNumber *port = url.port ?: [url.scheme isEqualToString:@"https"] ? @(443) : @(80);

struct sockaddr_in address;
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons([port intValue]);
address.sin_addr.s_addr = inet_addr([host UTF8String]);

reachability = [self reachabilityWithAddress:&address];
}
else
{
reachability = [self reachabilityWithHostname:host];
}

return reachability;
}

+(BOOL)isIpAddress:(NSString*)host
{
struct in_addr pin;
return 1 == inet_aton([host UTF8String], &pin);
}


// Initialization methods

Expand Down

0 comments on commit ee2ae91

Please sign in to comment.