Sara Canducci
Sara Canducci

Reputation: 6291

Get IP Address on iPhone | Objective-C or Swift

I just found this code to get my iPhone's IP Address using Objective-C but it's kinda slow for the project I'm working on:

- (NSString*)getExternalIP{
NSString *externalIP = @"";
NSArray *ipItemsArray = [[NSArray alloc] init];
NSURL *iPURL = [NSURL URLWithString:@"http://www.dyndns.org/cgi-bin/check_ip.cgi"];
if (iPURL) {
    NSError *error = nil;
    NSString *theIpHtml = [NSString stringWithContentsOfURL:iPURL encoding:NSUTF8StringEncoding error:&error];

    if (!error){
        NSScanner *theScanner;
        NSString *text = nil;
        theScanner = [NSScanner scannerWithString:theIpHtml];
        while ([theScanner isAtEnd] == NO) {
            [theScanner scanUpToString:@"<" intoString:NULL] ;
            [theScanner scanUpToString:@">" intoString:&text] ;
            theIpHtml = [theIpHtml stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@" "] ;
            ipItemsArray = [theIpHtml componentsSeparatedByString:@" "];
            int an_Integer= (int)[ipItemsArray indexOfObject:@"Address:"];
            externalIP =[ipItemsArray objectAtIndex: ++an_Integer];
        }
        return externalIP;
    }
}
return @"error";}

Do you know an alternative solution or a way to fix this one? NOTE: it has to work on Wi-fi and cellular.

Thanks!

Upvotes: 1

Views: 3311

Answers (2)

Scorchio
Scorchio

Reputation: 2871

Before using the code in the question, think about this for a bit:

What's going to happen if dyndns.org starts to lag?

In my experience, the response time of this service varies so badly that it's downright unusable for anything which might be performance sensitive. (I had response times varying from 0 sec to 50+ sec, tested on multiple decent connections.) It would be much better to find / set up a service for this purpose which is more reliable. If you have control over a web service for the application you're working with, I think it's trivial to add. (In PHP, it's a one-liner.)

Upvotes: 0

Developer
Developer

Reputation: 65

I am found ip address using this way. First you can import two library.

#include <ifaddrs.h>
#include <arpa/inet.h>

then after you can use this action

- (NSString *)getIPAddress {

    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            if(temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];

                }

            }

            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    return address;

} 

and call this action in your code like this way.

NSString *strYourIP = [self getIPAddress];

I hope this is use full to you....

Upvotes: 3

Related Questions