In this application we will see how to fetch data from server and replace it to the view in iPhone. This is very simple example. So let see how it will work.

Step 1: Open a Xcode, Create a View base application. Give the application name ”ReceiveData”.

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: We need to add one UIViewController class in the project. So select the project -> New File -> Cocoa Touch -> ViewController subclass and give the class name “DisplayData”.

Step 4: In the ReceiveDataViewController.h file, we need to define DisplayData class , create instance of  UIButton class and define one IBAction: method. So make the following changes:

#import <UIKit/UIKit.h>
@class DisplayData;

@interface ReceiveDataViewController : UIViewController {

UIButton *button;
DisplayData *displayData;

}

@property(nonatomic,retain) IBOutlet UIButton *button;

-(IBAction)DisplayData:(id)sender;

@end

Step 5: Double click the ReceiveDataViewController.xib file and open it to the Interface Builder. First drag the button from the library and place it to the view window. Select the UIButton and bring up Connection Inspector, connect Touch Up Inside to File’s Owner icon and select DisplayData: method. Now save the Interface Builder and go back to the Xcode.

Step 6: Open the ReceiveDataViewController.m file and make the following changes:

#import "ReceiveDataViewController.h"
#import "DisplayData.h"

@implementation ReceiveDataViewController

@synthesize button;

-(IBAction)DisplayData:(id)sender
{
displayData = [[DisplayData alloc]
initWithNibName:@"DisplayData"
bundle:nil];
[self.view addSubview:displayData.view];
}

- (void)dealloc
{
[super dealloc];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

#pragma mark – View lifecycle

- (void)viewDidUnload
{
[super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 7: Now open DisplayData.h file and make the following changes:

#import <UIKit/UIKit.h>

@interface DisplayData : UIViewController {

NSMutableData *webData;
UIScrollView *titleScrollView;
UITextView *textView;
UIActivityIndicatorView* loadIndicator;
}

@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic,retain) IBOutlet    UIScrollView *titleScrollView;
@property (nonatomic, retain) UIActivityIndicatorView *loadIndicator;
- (void)ActivityIndigator;

@end

Step 8: Double click the DisplayData.xib file and open it to the Interface Builder, first drag the scrollview from the library and place it to the view window and connect File’s Owner icon to the view window and select  titleScrollView. Now save the .xib file and go back to the Xcode.

Step 9: Open the DisplayData.m file and make the following changes:

#import "DisplayData.h"

@implementation DisplayData

@synthesize webData,titleScrollView,loadIndicator;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)dealloc
{
[super dealloc];
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren’t in use.
}

#pragma mark – View lifecycle

- (void)ActivityIndigator
{
loadIndicator =
[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(130, 200, 40, 40)];
loadIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;

[loadIndicator startAnimating];

[self.view addSubview:loadIndicator];

[loadIndicator release];

}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

[self ActivityIndigator];
NSURL *url = [NSURL URLWithString:@"http://www.chakrainteractive.com/mob//ReceiveData/Data.txt"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
webData = [[NSMutableData data] retain];

}
else
{

}

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

[webData appendData:data];

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message : @"An error has occured.Please verify your internet connection."
delegate:nil
cancelButtonTitle :@"OK"
otherButtonTitles :nil];
[alert show];
[alert release];

[loadIndicator removeFromSuperview ];

[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

NSLog(@"DONE. Received Bytes: %d", [webData length]);

NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 320,400)]; //size.height-30 )];
textView.text = loginStatus;
[textView setFont:[UIFont systemFontOfSize:14]];
[textView setBackgroundColor:[UIColor clearColor]];
[textView setTextColor:[UIColor blackColor]];
textView.editable = NO;
textView.dataDetectorTypes = UIDataDetectorTypeNone;

[loadIndicator removeFromSuperview ];

[titleScrollView addSubview:textView];

[loginStatus release];

[textView release];

[connection release];
[webData release];

}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 10: Now Compile and run the application in Simulator.

You can Download SourceCode from here ReceiveData

Did you enjoy this article?
Share
the
Love
Get Free Updates