Internet file reading problem

Discussion in 'iOS Programming' started by sagarshivam, Jun 30, 2011.

  1. sagarshivam macrumors member

    Joined:
    May 24, 2011
    #1
    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. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    There are delegate methods for that ;) I think the moment u are trying to display the data received. It's just not finished loading yet.
    Try looking up the delegate methods and work upon those.
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    Post the code that shows the NSURLConnection and NSMutableData being used in delegate methods for URL loading. If you implemented the delegate methods for URL loading, you need to show their code. If you didn't implement those, you need to do that.

    If you're not following a book or tutorial on reading data from network URLs, then you should be.

    At the very least, you must read and understand the "URL Loading System Programming Guide" document:
    http://developer.apple.com/library/...eptual/URLLoadingSystem/URLLoadingSystem.html

    You should also review at least one of the sample code projects associated with NSURLConnection, so you understand how asynchronous URL loading actually works.
     
  4. sagarshivam thread starter macrumors member

    Joined:
    May 24, 2011
    #4
    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'
    
    
    
    
    I need help here so that I can proceed further.Plz help
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    Declare startAsyncLoad:tag: either in the @interface of this class's .h file, or in a "private" category in the .m file.


    Presumably CustomURLConnection is a class you've created. Include the header file for the class at the top of this .m file.


    It seems you've declared the receivedData ivar incorrectly as an NSMutableData*, instead of NSMutableDictionary*. Change that in this class's @interface.


    I think the others are follow-on errors which will be resolved by fixing the above.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Alternatively, you could just place the code for it somewhere before the load method.
     
  7. sagarshivam thread starter macrumors member

    Joined:
    May 24, 2011
    #7
    thanks to all..was just able to solve the problem.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    So, you could say that this issue is resolved? (Hint, hint).
     

Share This Page