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.
@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.
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










