Thursday, March 10, 2011

Find External IP Address in iPhone SDK

NSURL *iPURL = [NSURL URLWithString:@""];
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) {
        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL] ;
        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text] ;
        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        theIpHtml = [theIpHtml stringByReplacingOccurrencesOfString:
        [NSString stringWithFormat:@"%@>", textwithString:@" "] ;
        ipItemsArray [theIpHtml componentsSeparatedByString:@" "];
        an_Integer=[ipItemsArray indexOfObject:@"Address:"];
        externalIP =[ipItemsArray objectAtIndex: ++an_Integer];
    } else {
        NSLog(@"Oops... g %d, %@",
        [error code],
        [error localizedDescription]);

1 comment:

holy joly said...

Nice information.I had checked my External ip address in my iPhone using this site