- Joined
- Jun 3, 2011
- Messages
- 54
- Reaction score
- 0
- Points
- 6
Dear All!
I just started doing good with the help of this forum.
In my first app,
I am creating one button and on clicking on that, a file (file name is hard coded) lying on the same machine/ lying over some remote server should be displayed.
I created button as:
Now I wrote two different method one for reading local files and one for getting content of a web page and calling them as button event which has been generated as above.
and the other method is
When I am passing "buttonPressed_local_file" as argument to action method in button created, file is read well and contents are displayed on console.
When I am passing "buttonPressed_internet_file" s argument to action method in button created, message as "connection successful " comes but length of 'receivedData' returned is 0 bytes.
Plz help me in reading the internet files.
I am very new to app development, so doing basic code only.
I just started doing good with the help of this forum.
In my first app,
I am creating one button and on clicking on that, a file (file name is hard coded) lying on the same machine/ lying over some remote server should be displayed.
I created button as:
Code:
- (void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = contentView;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 170, 100, 30);
[button setTitle:@"Read File" forState:UIControlStateNormal];
[button addTarget:self action:@selector(method_name)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
Now I wrote two different method one for reading local files and one for getting content of a web page and calling them as button event which has been generated as above.
Code:
- (void) buttonPressed_local_file {
NSFileHandle *infile;
infile = [NSFileHandle fileHandleForReadingAtPath: @"/Users/sagar/IB.txt"];
if (infile == nil) {
NSLog (@"reading operation failed");
}
else {
NSString *fileString = [NSString stringWithContentsOfFile: @"/Users/sagar/IB.txt"];
NSLog(@"%@", fileString);
NSLog (@"reading of the file was successful");
}
[infile closeFile];
}
and the other method is
Code:
- (void) buttonPressed_internet_file {
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSMutableData *receivedData;
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
NSLog(@"connection successfull");
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
}
else {
NSLog(@"connection failed");
}
}
When I am passing "buttonPressed_local_file" as argument to action method in button created, file is read well and contents are displayed on console.
When I am passing "buttonPressed_internet_file" s argument to action method in button created, message as "connection successful " comes but length of 'receivedData' returned is 0 bytes.
Plz help me in reading the internet files.
I am very new to app development, so doing basic code only.