New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
iOS Development Discussion on developing apps for the iOS platform.

Internet file reading problem


Post Reply New Thread Subscribe

 
Thread Tools
sagarshivam

 
Member Since: Jun 03, 2011
Posts: 54
sagarshivam is on a distinguished road

sagarshivam is offline
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.
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,092
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
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.

My Macs: Late 2013 rMBP w/ 750m; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Debian as a server; Apple TV (1080p version)
QUOTE Thanks
sagarshivam

 
Member Since: Jun 03, 2011
Posts: 54
sagarshivam is on a distinguished road

sagarshivam is offline
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'
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,092
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
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.

My Macs: Late 2013 rMBP w/ 750m; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Debian as a server; Apple TV (1080p version)
QUOTE Thanks
sagarshivam

 
Member Since: Jun 03, 2011
Posts: 54
sagarshivam is on a distinguished road

sagarshivam is offline
Thanks all!!

It was some code writing mistake..Everything is working fine now
QUOTE Thanks

Post Reply New Thread Subscribe


« UITextView problem | Scaling PHP/JSON? A "Delta-system" for iPhone? »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Problem with preview of .Nef file in Bridge Pierpi Images, Graphic Design, and Digital Photography 5 03-29-2011 08:59 AM
Internet Sharing Problem cng11 Apple Notebooks 0 10-12-2010 10:12 AM
Problem with internet connection vinoureux Internet, Networking, and Wireless 0 10-05-2010 08:55 AM
Internet Browser Problem James.0 OS X - Operating System 0 07-17-2010 04:17 AM
Airport Extreme and Internet connection problem BigMike Internet, Networking, and Wireless 1 09-10-2008 03:37 PM

All times are GMT -4. The time now is 06:12 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?