Results 1 to 5 of 5
  1. #1


    Member Since
    Jun 03, 2011
    Posts
    54
    Internet file reading problem
    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.

  2. #2


    Member Since
    Feb 25, 2009
    Posts
    2,112
    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.
    My Macs: Late 2013 rMBP w/ 750m, 16Gig ram; 2013 Mac Pro 6 core w/ D700, 16Gig Ram; 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 Linux; Apple TV (1080p version)

  3. #3


    Member Since
    Jun 03, 2011
    Posts
    54
    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'

  4. #4


    Member Since
    Feb 25, 2009
    Posts
    2,112
    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.
    My Macs: Late 2013 rMBP w/ 750m, 16Gig ram; 2013 Mac Pro 6 core w/ D700, 16Gig Ram; 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 Linux; Apple TV (1080p version)

  5. #5


    Member Since
    Jun 03, 2011
    Posts
    54
    Thanks all!!

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Reading an Outlook PST file without windows
    By GlobalNerds in forum OS X - Apps and Games
    Replies: 1
    Last Post: 09-13-2012, 11:53 PM
  2. Top file transferring/reading app
    By s6nculve in forum iOS and Apps
    Replies: 4
    Last Post: 02-10-2010, 11:22 PM
  3. Reading XLS file
    By Hunter in forum OS X - Apps and Games
    Replies: 5
    Last Post: 04-22-2009, 02:22 PM
  4. Reading pre OS X file systems?
    By Village Idiot in forum Running Windows (or anything else) on your Mac
    Replies: 2
    Last Post: 03-07-2008, 04:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •