frankyue
frankyue

Reputation: 31

Is there a way to get the ip address from given url in Cocoa?

Given a URL like: https://dl.google.com/chrome/mac/stable/GGRM/googlechrome.dmg, how can I get the IP address, e.g., 74.125.224.140, in the Cocoa framework or with an Objective-C method?

Upvotes: 0

Views: 2703

Answers (3)

Shelly Pritchard
Shelly Pritchard

Reputation: 11544

This worked for me

#import <netdb.h>
#include <arpa/inet.h>

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    struct hostent *host_entry = gethostbyname(charUrl);
    char *buff = inet_ntoa(*((struct in_addr *)host_entry->h_addr_list[0]));
});

Thanks to: source

Upvotes: 0

hkb_dev
hkb_dev

Reputation: 379

try this. I think that is what you expecting.

  1. It will convert string to url.
  2. From this url you can get domain name.
  3. From domain name you can get address as given below.

    NSURL *validURL = [NSURL URLWithString: yourUrl];
    NSString *host = [validURL host];
    NSString *ipAdress = [[NSHost hostWithName:host]address];
    

Upvotes: 4

VenoMKO
VenoMKO

Reputation: 3294

Try this:

NSString *ip = [[NSHost hostWithName:(NSString *)yourDomainNameUrl] address];

Upvotes: 2

Related Questions