Skip to main content

Posts

Showing posts with the label nsurlconnection

How to download a large file with the iPhone SDK and avoid memory usage issues?

I'm using the NSURLConnection class to download a large file in my iPhone application, but it crashes every so often because it's using too much memory. I'm doing the usual NSURLConnection usage, to append the received data to a NSMutableData object. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.fileData appendData:data]; } Then after I finish downloading the whole file, I save it to a local temporary file, and read it as a mapped file like this: - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // save the downloaded data into a temporary file NSString *tempPath = NSTemporaryDirectory(); NSString *tempFile = [tempPath stringByAppendingPathComponent:@"temp.pdf"]; [self.fileData writeToFile:tempFile atomically:YES]; NSData *mappedData = [NSData dataWithContentsOfMappedFile:tempFile]; NSURL *baseURL = [NSURL URLWithString:@"http://mydomain.com"]; [webView loadD

Loading NSData into a UIWebView

In my web browser, I am trying to load a UIWebView with NSData obtained from an NSURLConnection. When I try to load it into the UIWebView, instead of the site, it comes up with the HTML plain text. Here is my code: in viewDidLoad: NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.msn.com"]]; [NSURLConnection connectionWithRequest: request delegate:self]; later in the code: -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ webdata = [NSMutableData dataWithData: data]; } -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ [webview loadData:webdata MIMEType: @"text/html" textEncodingName: @"UTF-8" baseURL:nil]; }

connectionDidFinishLoading called before didReceiveData with rails production sending photos

I have a rails 3.0.3 website that, in development mode, responds to a HTTP GET by calling didReceiveData and then connectionDidFinishLoading, and photo data is sent from the website to the iPhone and displayed correctly. But when I run the rails 3.0.3 website in production mode, connectionDidFinishLoading is called before didReceiveData. I'm using carrierwave to handle the photos for both the development and production rails sites. Does anyone have any idea what could cause connectionDidFinishLoading to get called before didReceiveData? I've tried checking the following: 1) I made sure the NSURLConnectionDelegate is called for the class' interface. 2) didReceiveResponse gets called before connectionDidFinishLoading and the statusCode = 200. 3) In the rails app I am using send_file to send the photo data, and the path to the file sent in production, is an actual photo. 4) I checked that data was being returned from the production website by using the code below