IP_Address Check in iPhone

We will see in this example how to check IP Address in the iPhone. You can get easily the IP Address using this application.

We will see in this example how to check IP Address in the iPhone. You can get easily the IP Address using this application.

Step 1: Create a View-base application using template . Give the application name IP Address Check.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Expand classes and notice Interface Builder created the IP_Address_CheckViewController class for you. Expand Resources and notice the template generated a separate nib,IP_Address_CheckViewController.xib for the IP_Address_CheckViewController.

Step 4: Open IP_Address_CheckViewController , add an UITextView for displaying the text . Make the following changes in the IP_Address_CheckViewController.h file.

#import <UIKit/UIKit.h>

@interface IP_Address_CheckViewController : UIViewController {
IBOutlet UITextView *textView;
}

Step 5: Double click the IP_Address_CheckViewController.xib file open it to the Interface Builder. Drag text view from the library and place it in the view window. Connect the File’s Owner to the TextView select textView. Save it and go back to the Xcode.

Step 6: In the IP_Address_CheckViewController.m file , make the following changes.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
                textView.text = @"";
        }
        return self;
}

-(NSString*)getAddress {
char iphone_ip[255];
strcpy(iphone_ip,"127.0.0.1"); // if everything fails
NSHost* myhost =[NSHost currentHost];
if (myhost)
{
    NSString *ad = [myhost address];
  if (ad)
   strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);
}
  return [NSString stringWithFormat:@"%s",iphone_ip];
}

- (void)viewDidLoad {
   NSString* address = [self getAddress];
   NSString* myIPAdress = [NSString stringWithFormat:@"IP Address: %@", address];
   textView.text = myIPAdress;
}

Step 7: Now compile and run the application, but we have to see output in the iPhone. It is not showing the actual output in the simulator.

Figure 1: The Simulator output

You can downloaded SourceCode from here IP Address Check 2

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG