Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2012, 01:28 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 39
troop231 is on a distinguished road
Default File not downloading from server

This code shows through the NSLog that it downloaded the file, but when I check the Documents directory of the app on the simulator, there is no file to be found, can someone explain to me what's wrong with this code?

Thanks in advance

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Test";
	
    
    NSString *urlString = @"http://www.insertwebsitenamehere.com/test/intro.pdf";  
    NSLog(@"Downloading HTTP header from: %@", urlString);  
    NSURL *url = [NSURL URLWithString:urlString];  
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    

    NSString *cachedPath = [paths objectAtIndex:0]; 
    NSFileManager *fileManager = [NSFileManager defaultManager];  
    
    BOOL downloadFromServer = NO;  
    NSString *lastModifiedString = nil;  
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
    [request setHTTPMethod:@"HEAD"];  
    NSHTTPURLResponse *response;  
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];  
    if ([response respondsToSelector:@selector(allHeaderFields)]) {  
        lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"];  
    }  
    
    NSDate *lastModifiedServer = nil;  
    @try {  
        NSDateFormatter *df = [[NSDateFormatter alloc] init];  
        df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";  
        df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];  
        df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];  
        lastModifiedServer = [df dateFromString:lastModifiedString];  
        [df release];  
    }  
    @catch (NSException * e) {  
        NSLog(@"Error parsing last modified date: %@ - %@", lastModifiedString, [e description]);  
    }  
    NSLog(@"lastModifiedServer: %@", lastModifiedServer);  
    
    NSDate *lastModifiedLocal = nil;  
    if ([fileManager fileExistsAtPath:cachedPath]) {  
        NSError *error = nil;  
        NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:cachedPath error:&error];  
        if (error) {  
            NSLog(@"Error reading file attributes for: %@ - %@", cachedPath, [error localizedDescription]);  
        }  
        lastModifiedLocal = [fileAttributes fileModificationDate];  
        NSLog(@"lastModifiedLocal : %@", lastModifiedLocal);  
    }  
    
    // Download file from server if we don't have a local file  
    if (!lastModifiedLocal) {  
        downloadFromServer = YES;  
    }  
    // Download file from server if the server modified timestamp is later than the local modified timestamp  
    if ([lastModifiedLocal laterDate:lastModifiedServer] == lastModifiedServer) {  
        downloadFromServer = YES;  
    }  
    
    if (downloadFromServer) {  
        NSLog(@"Downloading new file from server");  
        NSData *data = [NSData dataWithContentsOfURL:url];  
        if (data) {  
            // Save the data  
            if ([data writeToFile:cachedPath atomically:YES]) {  
                NSLog(@"Downloaded file saved to: %@", cachedPath);  
            }  
            
            // Set the file modification date to the timestamp from the server  
            if (lastModifiedServer) {  
                NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:lastModifiedServer forKey:NSFileModificationDate];  
                NSError *error = nil;  
                if ([fileManager setAttributes:fileAttributes ofItemAtPath:cachedPath error:&error]) {  
                    NSLog(@"File modification date updated");  
                }  
                if (error) {  
                    NSLog(@"Error setting file attributes for: %@ - %@", cachedPath, [error localizedDescription]);  
                }  
            }  
        }  
    }  
}

Last edited by troop231; 02-07-2012 at 01:36 PM.
troop231 is offline   Reply With Quote
Old 02-10-2012, 12:15 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 94
JamesCahall is on a distinguished road
Default

Quote:
Originally Posted by troop231 View Post
This code shows through the NSLog that it downloaded the file, but when I check the Documents directory of the app on the simulator, there is no file to be found, can someone explain to me what's wrong with this code?

Thanks in advance

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Test";
	
    
    NSString *urlString = @"http://www.insertwebsitenamehere.com/test/intro.pdf";  
    NSLog(@"Downloading HTTP header from: %@", urlString);  
    NSURL *url = [NSURL URLWithString:urlString];  
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    

    NSString *cachedPath = [paths objectAtIndex:0]; 
    NSFileManager *fileManager = [NSFileManager defaultManager];  
    
    BOOL downloadFromServer = NO;  
    NSString *lastModifiedString = nil;  
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
    [request setHTTPMethod:@"HEAD"];  
    NSHTTPURLResponse *response;  
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];  
    if ([response respondsToSelector:@selector(allHeaderFields)]) {  
        lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"];  
    }  
    
    NSDate *lastModifiedServer = nil;  
    @try {  
        NSDateFormatter *df = [[NSDateFormatter alloc] init];  
        df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";  
        df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];  
        df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];  
        lastModifiedServer = [df dateFromString:lastModifiedString];  
        [df release];  
    }  
    @catch (NSException * e) {  
        NSLog(@"Error parsing last modified date: %@ - %@", lastModifiedString, [e description]);  
    }  
    NSLog(@"lastModifiedServer: %@", lastModifiedServer);  
    
    NSDate *lastModifiedLocal = nil;  
    if ([fileManager fileExistsAtPath:cachedPath]) {  
        NSError *error = nil;  
        NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:cachedPath error:&error];  
        if (error) {  
            NSLog(@"Error reading file attributes for: %@ - %@", cachedPath, [error localizedDescription]);  
        }  
        lastModifiedLocal = [fileAttributes fileModificationDate];  
        NSLog(@"lastModifiedLocal : %@", lastModifiedLocal);  
    }  
    
    // Download file from server if we don't have a local file  
    if (!lastModifiedLocal) {  
        downloadFromServer = YES;  
    }  
    // Download file from server if the server modified timestamp is later than the local modified timestamp  
    if ([lastModifiedLocal laterDate:lastModifiedServer] == lastModifiedServer) {  
        downloadFromServer = YES;  
    }  
    
    if (downloadFromServer) {  
        NSLog(@"Downloading new file from server");  
        NSData *data = [NSData dataWithContentsOfURL:url];  
        if (data) {  
            // Save the data  
            if ([data writeToFile:cachedPath atomically:YES]) {  
                NSLog(@"Downloaded file saved to: %@", cachedPath);  
            }  
            
            // Set the file modification date to the timestamp from the server  
            if (lastModifiedServer) {  
                NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:lastModifiedServer forKey:NSFileModificationDate];  
                NSError *error = nil;  
                if ([fileManager setAttributes:fileAttributes ofItemAtPath:cachedPath error:&error]) {  
                    NSLog(@"File modification date updated");  
                }  
                if (error) {  
                    NSLog(@"Error setting file attributes for: %@ - %@", cachedPath, [error localizedDescription]);  
                }  
            }  
        }  
    }  
}
It appears your cached path is missing a filename like so:

cachedPath = [cachedPath stringByAppendingPathComponent:@"intro.pdf"];

James
JamesCahall is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On



» Advertisements
» Online Users: 400
13 members and 387 guests
7twenty7, chiataytuday, cristofercolmbos, fiftysixty, gmarro, iOS.Lover, KennyChong, kilobytedump, Matrix23, raymng, ryantcb, stanny, xerohuang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:19 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0