views:

26

answers:

0

Hi all,

I have an app in which I download file using NSURLConnection class object and the code works well on the iphone device 3.0 but when I run the same code for a different build on iPad it doesn't. So I want to ask are there any changes have been done in iPhone OS 3.2 related to NSURLConnection or something else is the prob.

Here is my code:

 -(IBAction) download:(id)sender
{

    if([urlTextField.text length] > 0)
    {

        NSString *feedURLString = [[NSString alloc] initWithString:urlTextField.text];
        NSURL * url = [[NSURL alloc] initWithString:feedURLString];
        NSURLRequest *historyURLRequest = [[NSURLRequest alloc] initWithURL:url];
        NSURLConnection *historyDataFeedConnection = [[NSURLConnection alloc] initWithRequest:historyURLRequest delegate:self];

        [feedURLString release];

        [url release];
        [historyURLRequest release]; 
        [historyDataFeedConnection release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Empty Fields" message:@"Please Enter a valid URL" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

}



#pragma mark NSURLConnection delegate methods


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {


}

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    if(response == nil)
        response = [[NSMutableData alloc] initWithData:data];
    else
        [response appendData:data];



}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;   
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Connection Error!" message:@"Could not connect to server" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];

 }

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {


    BOOL downloadFileExist = NO;

    NSString *temp = [[urlTextField.text componentsSeparatedByString:@"/"] lastObject];
    fileName = [temp stringByReplacingOccurrencesOfString:@"-" withString:@""];
    [fileName retain];

    if ([fileInformationArray count] > 0) {

        for (int i=0; i < [fileInformationArray count]; i++) {
            if ([[[fileInformationArray objectAtIndex:i] objectForKey:@"fileName"] isEqualToString:fileName]) {

                printf("\n Downloaded File Already Exist");
                downloadFileExist = YES;

                [self stopActivityIndicator];
            }
        }
    }

    if (!downloadFileExist) {

        NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [array objectAtIndex:0];

        NSString *temp = [documentsDirectory stringByAppendingPathComponent:fileName];

        [temp retain];

        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager createFileAtPath:temp contents:[NSData dataWithData:response] attributes:nil]) {
            printf("\nfile Created");
        }
        [temp release];
        [NSThread detachNewThreadSelector:@selector(getFileInformation:) toTarget:self withObject:fileName];
    }

    [response release];
    response = nil; 



   }