Internet file reading problem

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:
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.
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
You need to implement the delegate methods that actually will store the data.

You need to store the NSMutableData object as an IVar and not a local variable in that method that you create the request.

Since this is an ASYNC request, data will be received in the background while your app is allowed to continue to run. The delegate methods give you the ability to store the data as it arrives, reset the data when appropriate, complete the process when appropriate and provide error messages should there be a problem.

You need to read this page:

Loading…

that will give detailed information on the delegate methods you need to implement.

Just remember, you won't have valid data where you're currently doing the length test.
 
OP
S
Joined
Jun 3, 2011
Messages
54
Reaction score
0
Points
6
Still no solution!!

Dear all!
Still could not make a point to get my work done!

I am just explaining part of it where I am feeling difficulty.
Right now, I just want some file (say .txt file ) to be read from some server and displayed it on debugger console (right now not on UItextview or anywhere else).

I was able to manage a decent GUI (as I am really a newbie for all these). Now as I could see that delegate methods are required to serve my purpose.

Part of code where I create one button on clicking which "load " function should be called.
Code:
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
							initWithTitle:@"newbutton" 
							style:UIBarButtonItemStylePlain 
							target:self 
							action:@selector(load)] autorelease];

and no complain here.
In the same .m file , I am defining further as :

Code:
- (void)load {
			receivedData = [[NSMutableDictionary alloc] init];
			
			NSURL *url1 = [NSURL URLWithString:@"http://172.22.79.169/~spp/test.txt"];
			
			
			[self startAsyncLoad:url1 tag:@"tag1"];
			
		}

		- (void)startAsyncLoad:(NSURL*)url tag:(NSString*)tag {
			NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
			CustomURLConnection *connection = [[CustomURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES tag:tag];
			
			if (connection) {
				[receivedData setObject:[[NSMutableData data] retain] forKey:connection.tag];
			}
		}
		- (NSMutableData*)dataForConnection:(CustomURLConnection*)connection {
			NSMutableData *data = [receivedData objectForKey:connection.tag];
			return data;
		}
		
		- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
			NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
			[dataForConnection setLength:0];
		}
		
		- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
			NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
			[dataForConnection appendData:data];
		}
		
		- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
			NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
			[connection release];
			
		}


But this is not working!! Where exactly I am making mistakes.
I am even unable to build it successfully:

errors are:

Code:
/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:62:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:62: warning: 'noXIBViewController' may not respond to '-startAsyncLoad:tag:'


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:62:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:62: warning: (Messages without a matching method signature


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:63:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:63: warning: 'noXIBViewController' may not respond to '-startAsyncLoad:tag:'


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:64:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:64: warning: 'noXIBViewController' may not respond to '-startAsyncLoad:tag:'


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:68:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:68: error: 'CustomURLConnection' undeclared (first use in this function)


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:68:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:68: error: 'connection' undeclared (first use in this function)


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:71:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:71: warning: 'NSMutableData' may not respond to '-setObject:forKey:'


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:0:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m: At top level:


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:74:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:74: error: expected ')' before 'CustomURLConnection'


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:75:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:75: error: request for member 'tag' in something not a structure or union


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:75:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:75: warning: 'NSMutableData' may not respond to '-objectForKey:'


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:80:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:80: error: 'CustomURLConnection' undeclared (first use in this function)


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:80:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:80: error: expected expression before ')' token


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:85:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:85: error: 'CustomURLConnection' undeclared (first use in this function)


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:85:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:85: error: expected expression before ')' token


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:90:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:90: error: 'CustomURLConnection' undeclared (first use in this function)


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:90:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:90: error: expected expression before ')' token


/Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:90:0 /Users/sagar/Downloads/noXIB/Classes/noXIBViewController.m:90: warning: unused variable 'dataForConnection'
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
Most of those warnings and errors are fairly straight forward.

something like:
warning: 'noXIBViewController' may not respond to '-startAsyncLoad:tag:'
usually means that you don't have the method declared in the interface file - even if you have it implemented in the implementation, you still need to declare it in the interface (unless you're overriding a method from a parent class) otherwise the compiler will think the class won't respond to that method (it parses the interface to know the methods that are available).

error: 'CustomURLConnection' undeclared (first use in this function)
This doesn't exist as a class from Apple, and if you created your own class called CustomURLConnection, you didn't import the header for it in the implementation file that uses the class.

error: request for member 'tag' in something not a structure or union
You're attempting to use something that doesn't exist.

warning: 'NSMutableData' may not respond to '-objectForKey:'
There is no objectForKey to really use in NSMutableData - NSMutableData is really just a block of data for which you can plunk bytes into it, it's not a keyed storage (like a NSDictionary would be)

In all honesty, you've made something that should be easy very hard. You just need to use a NSURLConnection and implement the delegate methods that are appropriate - there are a ton of examples for it on the net and really, it's very easy to use.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top