I have always come across some students who ask me how to get the IPAddress of a computer and display it in the simulator, well this post is dedicated to all those students.
Design Phase: In this application we will have aUIButton on your window and on the hit of the UIButton we will display the IPAddress on a UILabel so the final design looks something like this
Step1: Open Xcode create a windows based application and add UIViewController subclass and name it myviewController, so now you will be having two files that’s myviewController.h and myviewController.m.
Step 2: select the myviewController.h file and create the objects of UIButton and UILabel here’s how you will do that
@interface myviewController : UIViewController {
UILabel *lbl;
UIButton *btn;
}
-(void) displayIP;
@end
Also we have a function which will get called when we touch the button and in this function we will perform the actual coding for getting the IPAddress
Step 3: Go to the myviewController.m file and give body to the UIButton and UILabel objects here’s the code to do that
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
//setting the button type
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//setting the button frame
[btn setFrame:CGRectMake(82, 83, 134, 37)];
//setting the button text
[btn setTitle:@"Get IP" forState:UIControlStateNormal];
//setting the action for the button touch
[btn addTarget:self action:@selector(displayIP) forControlEvents:UIControlEventTouchUpInside];
//setting the label frame
lbl = [[UILabel alloc]initWithFrame:CGRectMake(68, 159, 186, 21)];
//setting the label text
lbl.text = @"IP will appear here";
}
return self;
}
Step 4: Now we will code for the function which will display the IP and here’s how it looks
-(void) displayIP
{
lbl.text = [ [ [ NSHost currentHost ]addresses ]objectAtIndex:1 ];
}
Code Explanation: The NSHost class provides methods to access the networks name and address information for a host.
So I have used this class to get the current IP address and it has a class method called current host which will return you the data of the host in which your current application is running.
Address will give you all the network address of the host, address is an NSArray so just pick the data present in the first index and that data will be the IP Address of the computer in which this application is running in this case your computer.
Step 5: Go to the AppDelegate.m file and add this view to the window and press Build and Go in order to get the final view and here's the code to do that
#import "IPaddressAppDelegate.h"
#import "myviewController.h"
@implementation IPaddressAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
myviewController *obj = [[myviewController alloc]init];
[window addSubview:obj.view];
[window makeKeyAndVisible];
}
i need a program to send message from iphone simulator to another pc, by using socket programming please help me
ReplyDelete